Hi all, I am trying to input a few sentences and check if each sentence is a palindrome without the spaces, commas and other which means we only take into account of alphabets (a-z or A-Z) e.g.Code:#include <stdio.h> #define MAX_INPUT_LENGTH 80 int is_palindrome(char []); int main(void) { int i = 0, indicator = 0; char words[MAX_INPUT_LENGTH] = {'a'}; do { do { scanf("%c", &words[i]); i++; } while(words[i] != '\n'); indicator += is_palindrome(words); } while(words[i] != '\n' && words[i-1] != '\n'); return 0; } int is_palindrome(char arr[]) { int max, max_temp, i; for(i = 0; i < MAX_INPUT_LENGTH; i++) { if(arr[i] == '\n') { max = i - 1; max_temp = max; break; } } for(i = 0; i < max / 2 + 1; i++) { if(arr[i] != arr[max_temp]) return 0; else max_temp--; } return 1; }
A car, a man, a maraca.
A nut for a jar of tuna.
Are we not drawn onward to new era?
It should give output 3 e.g. the number of palindrome sentences entered.
A enter with nothing for the line will end it.
However, I am not sure how to filter out the spaces and other non-alphabets as inputs, plus my while statements which are aimed at ending inputs when there is an empty line does not seem to work but I felt that it would work. Anyone can explain to me why it does not work?



LinkBack URL
About LinkBacks



