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