C编程。 - 在一个句子中查找回文数量 - 如何设置循环以在执行计数时对单词进行标记

时间:2017-04-16 08:48:50

标签: c string palindrome word-count

我正在开发一个程序,根据用户输入的句子,检查句子中有多少个回文单词。

示例:

输入

  

gig school level bye

输出

  

2

问题:如何在执行计数时设置循环来标记单词?

注意:我不能使用strrev函数,我不能逐个插入单词(用户必须输入单个字符串)。

以下是我目前编码的内容。

/*
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;
}

3 个答案:

答案 0 :(得分:0)

您不应该使用gets,不推荐使用它。使用fgets。此外,你已经将j初始化为len-1(这是字符串的结尾)并且你将当前单词的开头与字符串的结尾进行比较而不是比较单词的结尾。你需要找到单词的结尾并检查。我已经编辑了一下你的代码,我能够看到所需的结果:

#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;
}

我希望这能让您轻微了解如何解决这个问题。

答案 1 :(得分:0)

你的逻辑不正确。我已经按照你的风格编写了代码,现在你可以轻松地理解这些代码并直接在任何你想要的地方使用这些代码。

检查此代码

#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;
}

答案 2 :(得分:-1)

这是我们的解决方案。我将句子标记为单词,然后我有一个函数来检查每个单词是否是回文,如果是,我递增计数器。

#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;
}