-1

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;
}
TourEiffel
  • 4,034
  • 2
  • 16
  • 45
gl3yn
  • 301
  • 1
  • 3
  • 13

3 Answers3

1

You should not be using gets, It's usage is deprecated. Use fgets.Moreover you have initialized j to len-1 (which is end of the string) and you're comparing current word's beginning to the string's end rather than comparing word's end. You need to find word's end and check. I've edited your code a bit and i was able to see the desired result :

#include <stdio.h>
#include <string.h>

int checkPalindrome(char s[], int start, int end) {
  while(start < end) {
    if(s[start] == s[end]) {
      start++;
      end--;
    }
    else
      return 0;
  }
  return 1;
}

int main()
{
   char s[80],temp;
   int i,j, len, count, nPalindromes = 0, start;
   count = 0;

   fgets(s, 80, stdin);
   len=strlen(s);
   s[len-1] = '\0';
   len--;
   j = len - 1;

   for(i = 0; i < len; ) {
     // skip whitespaces
     while(i < len && s[i] == ' ')
       i++;
     // find the other end of word
     start = i;
     while(i < len && s[i] != ' ') 
       i++;

     // check for palindrome 
     if(checkPalindrome(s, start, i-1) == 1)
       nPalindromes++;
   }

   printf("%d\n", nPalindromes);
   return 0;
}

I hope this gives you slight idea of how to approach this problem.

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
1

Your logic is not correct. I have written the code in your style and now you can easily understand the things and use this code directly wherever you want.

Check this code

#include<stdio.h>
#include<string.h>

int main()
{
  int i,j,k,l,len,count = 0;
  char s[80];

  gets(s);
  len = strlen(s);

  for(i=0;i<len;i++)
  {
    if(s[i] != ' ' || s[i] != '\n')
    {
      for(j=i;j<len;j++)
      {
          if(s[j] == ' ' || s[j] == '\n')
            break;
      }
    }

    for(k=i,l=j-1;;)
    {
      if(s[k] != s[l])
        break;
      else if(k >= l)
      {
        count = count + 1;
        break;
      }
      else
      {
        k = k + 1;
        l = l - 1;
        continue;
      }
    }

    i = j;
  }

  printf("\n%d",count);
  return 0;
}
Mahesh Suthar
  • 146
  • 1
  • 12
0

Here us a solution. I tokenize the sentence into words and then I have a function to check if each word is a palindrome, and if it is I increment the counter.

#include <stdio.h>
#include <string.h>

int isPalidrome(char * word, int length) {
    int i;

    for(i = 0; i < length / 2; i++) {
        if(word[i] != word[length - i - 1]) {
            return 0;
        }
    }

    return 1;
}

int main()
{
    char sentence[] = "gig school level bye";
    char * token = NULL;
    int count = 0;

    token = strtok(&sentence[0], " ");

    while(token != NULL) {
        if(isPalidrome(token, strlen(token))) {
            count++;
        }

        token = strtok(NULL, " ");
    }

    printf("%d", count);
    return 0;
}
Archmede
  • 1,592
  • 2
  • 20
  • 37