Posted By

jaircazarin on 10/10/08


Tagged

Algorithms Microsoft-Interview


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jaircazarin
sergeizen
skammer


Is Palindrome


 / Published in: C
 

Check if the given string is a palindrome.

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int isPalindrome(char *word)
  5. {
  6. if(NULL == word)
  7. return 0;
  8. int length = strlen(word);
  9. if(1 == length || 0 == length)
  10. return 1;
  11. int start = 0;
  12. while(word[start] == ' ')
  13. start++;
  14. int end = length - 1;
  15. while(word[end] == ' ')
  16. end--;
  17. for(int i = start, j = end; i<=j; i++, j--)
  18. {
  19. if(word[i] == ' ' || word[j] == ' ')
  20. return 0;
  21. if(word[i] != word[j])
  22. return 0;
  23. }
  24. return 1;
  25. }
  26.  
  27. int main()
  28. {
  29. printf("%d\n", isPalindrome(NULL));
  30. printf("%d\n", isPalindrome(""));
  31. printf("%d\n", isPalindrome("a"));
  32. printf("%d\n", isPalindrome("adadasfdf"));
  33. printf("%d\n", isPalindrome("abba"));
  34. printf("%d\n", isPalindrome("aba"));
  35. printf("%d\n", isPalindrome(" abba"));
  36. printf("%d\n", isPalindrome(" abba "));
  37. printf("%d\n", isPalindrome("ab ba"));
  38. return 0;
  39. }

Report this snippet  

You need to login to post a comment.