Advertisement
Slyfoxx724

p6.c

Dec 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. 1 #include <stdio.h>
  2. 2 #define TRUE 1
  3. 3 #define FALSE 0
  4. 4
  5. 5 void make_copy_of_string(char str[], char str_copy[]);
  6. 6 void keep_chars(char string[]);
  7. 7 void convert_upper_to_lower_case(char string[]);
  8. 8 _Bool palindromeness(char string[]);
  9. 9
  10. 10 int main (void)
  11. 11 {
  12. 12 char phrase[101], phrase_copy[101];
  13. 13
  14. 14 printf("Enter phrase:\n");
  15. 15 fgets(phrase, 101, stdin);
  16. 16
  17. 17 // printf("%s", phrase);
  18. 18
  19. 19 make_copy_of_string(phrase, phrase_copy);
  20. 20 keep_chars(phrase_copy);
  21. 21 convert_upper_to_lower_case(phrase_copy);
  22. 22
  23. 23 if(palindromeness(phrase_copy) == TRUE)
  24. 24 printf("The phrase: %s\nIs a palindrome!\n", phrase);
  25. 25 else
  26. 26 printf("Your phrase: %s\nIs not a palindrome!\n", phrase);
  27. 27
  28. 28
  29. 29 // printf("%s", phrase_copy);
  30. 30
  31. 31 return 0;
  32. 32
  33. 33 }
  34. 34
  35.  
  36. 35 void make_copy_of_string(char str[], char str_copy[])
  37. 36 {
  38. 37 int i=0;
  39. 38
  40. 39 while(str[i] != '\n' && str[i] != '\0')
  41. 40 {
  42. 41 str_copy[i] = str[i];
  43. 42 i++;
  44. 43 }
  45. 44 str_copy[i] = '\0';
  46. 45 str[i] = '\0';
  47. 46 }
  48. 47
  49. 48 void keep_chars(char string[])
  50. 49 {
  51. 50 int i=0, j=0;
  52. 51
  53. 52 while(string[i] != '\0')
  54. 53 {
  55. 54 if( ('A' <= string[i] && string [i] <= 'Z') || ('a' <= string[ i] && string[i] <= 'z'))
  56. 55 {
  57. 56 string[j] = string[i];
  58. 57 i++;
  59. 58 j++;
  60. 59 }
  61. 60 else
  62. 61 {
  63. 62 i++;
  64. 63 }
  65. 64 }
  66. 65
  67. 66 string[j] = '\0';
  68. 67 }
  69. 68
  70.  
  71. 69 void convert_upper_to_lower_case(char string[])
  72. 70 {
  73. 71 int i=0;
  74. 72
  75. 73 while(string[i] != '\-1')
  76. 74 {
  77. 75 if((string[i]>='A') && (string[i]<='Z'))
  78. 76 {
  79. 77 string[i]=string[i]+32;
  80. 78 i++;
  81. 79 }
  82. 80 else
  83. 81 {
  84. 82 i++;
  85. 83 }
  86. 84 }
  87. 85
  88. 86 }
  89. 87
  90. 88 _Bool palindromeness(char string[])
  91. 89 {
  92. 90 char reverse_string[100] = {'\0'};
  93. 91 int i=0, length=0;
  94. 92 _Bool palindrome;
  95. 93
  96. 94 for(i=0; string[i] != '\0'; i++)
  97. 95 length++;
  98. 96
  99. 97 for(i=length-1; i>=0; i--)
  100. 98 reverse_string[length-i-1] = string[i];
  101. 99
  102. 100 for(palindrome = TRUE, i=0; i<length; i++)
  103. 101 {
  104. 102 if(reverse_string[i] != string[i])
  105. 103 palindrome=FALSE;
  106. 104 }
  107. 105
  108. 106 return palindrome;
  109. 107 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement