I am working on a program which accepts a long string as input (a sentence). The program will check the string and count the number of palindrome words found then return the count.
Example:
input:
_gig school level bye_
output:
_2._
How can I get the total count of palindrome words in a sentence?
Below is what I have coded so far.
/*
My basic idea is:
1) I will retrieve words from a sentence using "if(s[i] == ' ')"
2) I will check if the word is a palindrome
3) If it is a palindrome, I will add the word to the count "count++"
*/
#include <stdio.h>
#include <string.h>
int main()
{
char s[80],temp;
int i,j, len, count;
count = 0;
gets(s);
len=strlen(s);
j = len - 1;
for(i = 0; i < len; i++){
if(s[i] == ' '){
for(;i<j;i++,j--)
{
if(s[i] != s[j])
break;
}
if(i>=j)
{
count++;
}
}
if (i == len)
break;
}
printf("%d", count);
return 0;
}