Palindrome Check, Dynamic Memory

panagos panagos is offline Offline 30 Days Ago, 5:00 pm |
0
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.

  1. a n n a
  2. |--|
  3. |-----|

Hope it's helpful...
Last edited by panagos; 30 Days Ago at 5:14 pm.
Quick reply to this message  
C Syntax
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int kark(char *);
  5. int main()
  6. {
  7. char *s;
  8. int count;
  9.  
  10. printf("give the length of the word:");
  11. scanf("%d",&count);
  12. s=(char *)malloc(count+1);
  13. printf("give word:");
  14. scanf("%s",s);
  15.  
  16. if(kark(s))/*==1)*/
  17. printf("\n\tThe word is palindromic.\n");
  18. else
  19. printf("\n\tThe word is NOT palindromic.\n");
  20.  
  21. return 0;
  22. }
  23.  
  24. int kark(char *x)
  25. {
  26. int i,j,flag;
  27.  
  28. flag=1;
  29. i=0;
  30. j=strlen(x)-1;
  31. while((flag==1)&&(x[i]!='\0'))
  32. {
  33.  
  34. if(x[i]==x[j])
  35. flag=1;
  36. else{
  37. flag=0;}
  38. j--;
  39. i++;
  40. }
  41.  
  42. return flag;
  43.  
  44. }

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC