Advertisement
Guest User

Untitled

a guest
May 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define false 0
  5. #define true 1
  6.  
  7. int isPalindrome(char str[], int beg, int end);
  8.  
  9. int main(void)
  10. {
  11.  
  12. int s = 0;
  13. char str[] = "abccba";
  14. s = isPalindrome("abccba", 0, 5);
  15. printf("%d", s);
  16.  
  17. getchar();
  18. return 0;
  19. }
  20.  
  21. /*
  22. function gets a string and begining and end points and checks if it is a palindrome or not
  23. input: the string, beg and end points
  24. output: if the string is palindrome or not
  25. */
  26. int isPalindrome(char str[], int beg, int end)
  27. {
  28. int palindrome;
  29.  
  30. if (strlen(str) % 2 != 0)
  31. {
  32. if (beg <= end)
  33. {
  34. if (beg == end)
  35. {
  36. return true;
  37. }
  38. if (str[beg] == str[end])
  39. {
  40. palindrome = isPalindrome(str, beg + 1, end - 1);
  41. }
  42. else if (str[beg] != str[end])
  43. {
  44.  
  45. return false;
  46. }
  47. }
  48. }
  49. else
  50. {
  51. if (beg <= end)
  52. {
  53. if (beg + 1 == end)
  54. {
  55. if (str[beg+1] == str[end])
  56. {
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. if (str[beg] == str[end])
  65. {
  66. palindrome = isPalindrome(str, beg + 1, end - 1);
  67. }
  68. else if (str[beg] != str[end])
  69. {
  70. return false;
  71. }
  72. }
  73. }
  74. return palindrome;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement