Palindrome Check, Dynamic Memory

panagos 0 Tallied Votes 237 Views Share

Just another way to check if a word is palindromic or not.
In my country (Greece) if a word is palindromic we say it is
"καρκινική" that's why i named the fuction in the code kark().

-What it does?

If we would say that word has n letters,then this
compares the first with the n letter, next the second with the n-1 letter and so on..
e.g. let's take the name "anna"
compares the first 'a' with the last 'a'
and the two 'n' in between.

a n n a
  |--|
|-----|

Hope it's helpful...:)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int kark(char *);
int main()
{
    char *s;
    int count;
    
    printf("give the length of the word:");
    scanf("%d",&count);
    s=(char *)malloc(count+1);
    printf("give word:");
    scanf("%s",s);

    if(kark(s))/*==1)*/
        printf("\n\tThe word is palindromic.\n");
    else
        printf("\n\tThe word is NOT palindromic.\n");

    return 0;
}

int kark(char *x)
{
    int i,j,flag;
   
    flag=1;
     i=0;
    j=strlen(x)-1;
    while((flag==1)&&(x[i]!='\0'))
    {
        
        if(x[i]==x[j])
        flag=1;
        else{
            flag=0;}
        j--;
        i++;
    }

    return flag;

}