繁体   English   中英

如何计算至少包含3个元音的单词数

[英]How to count the number of words that contain at least 3 vowels

我想知道是否可以寻求帮助。 我正在用C编写一个程序,该程序写出字符串中的字符,单词和元音的数量(并添加了一些打印语句)。 我试图弄清楚如何编写遍历字符串的代码并计算包含至少3个元音的单词的数量。 我觉得这似乎是很容易编写的代码,但这似乎总是让我感到最简单的事情。 有什么帮助吗?

另外:是C语言的新手,我如何在使用int vowel_count(char my_sen[])函数而不是在main中使用代码时获得相同的结果?

如果这有点令人困惑,因为我的主代码已经包含用于计算输入中元音数量的代码,我该如何在某种程度上将所说的代码转移到该函数中并仍然在main中调用它?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SENTENCE 256


int main(void){

char my_sen[SENTENCE], *s; //String that containts at most 256 as well as a pointer
int words = 1, count = 0,vowel_word = 0; //Integer variables being defined
int i,vowel = 0, length;  //More definitions
printf("Enter a sentence: ");//Input sentence
gets(my_sen);//Receives and processes input
length = strlen(my_sen); //Stores the length of the input within length

for(i=0;my_sen[i] != '\0'; i++){
    if(my_sen[i]=='a' || my_sen[i]=='e' || my_sen[i]=='i' || my_sen[i]=='o' || my_sen[i]=='u' || //Loop that states if the input contains any of the following
       my_sen[i]=='A' || my_sen[i]=='E' || my_sen[i]=='I' || my_sen[i]=='O' || my_sen[i]=='U')   //characters(in this case, vowels), then it shall be
       {                                                                                         //stored to be later printed
           vowel++;
       }


    if(my_sen[i]==' ' || my_sen[i]=='!' || my_sen[i]=='.' || my_sen[i]==',' || my_sen[i]==';' || //Similar to the vowel loop, but this time
        my_sen[i]=='?')                                                                          //if the following characters are scanned within the input
        {                                                                                        //then the length of the characters within the input is
            length--;                                                                            //subtracted

                    }

}


for(s = my_sen; *s != '\0'; s++){ //Loop that stores the number of words typed after
    if(*s == ' '){                //each following space
    count++;
}
}


printf("The sentence entered is %u characters long.\n", length); //Simply prints the number of characters within the input
printf("Number of words in the sentence: %d\n", count + 1); // Adding 1 to t[he count to keep track of the last word
printf("Average length of a word in the input: %d\n", length/count);//Prints the average length of words in the input
printf("Total Number of Vowels: %d\n", vowel);//Prints the number of vowels in the input
printf("Average number of vowels: %d\n", vowel/count);//Prints the average number of vowels within the input
printf("Number of words that contain at least 3 vowels: %d\n", vowel_word);//Prints number of words that contain at least 3 vowels
return 0;
}

这不是什么大问题。

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

int vowel_count(char my_sen[])
{
  int wcount = 0; // number of words with 3+ vowel chars
  int vcount = 0; // current number of vowel chars in the current word
  int i = 0; // index into the string
  int ch;
  while ((ch = my_sen[i++]) != '\0')
  {
    if (isspace(ch) || !isalpha(ch))
    {
      // ch is not an alphabetical char, which can happen either
      // before a word or after a word.
      // If it's after a word, the running vowel count can be >= 3
      // and we need to count this word in.
      wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
      vcount = 0; // reset the running vowel counter
      continue; // skip spaces and non-alphabetical chars
    }
    if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these
    {
      ++vcount; // count vowels
    }
  }
  // If my_sen[] ends with an alphabetical char,
  // which belongs to the last word, we haven't yet
  // had a chance to process its vcount. We only
  // do that in the above code when seeing a non-
  // alphabetical char following a word, but the
  // loop body doesn't execute for the final ch='\0'.
  wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
  return wcount;
}

int main(void)
{
  char sen[] = "CONSTITUTION: We the People of the United States...";
  printf("# of words with 3+ vowels in \"%s\" is %d", sen, vowel_count(sen));
  return 0;
}

输出( ideone ):

# of words with 3+ vowels in "CONSTITUTION: We the People of the United States..." is 3

顺便说一句,您可以更改此功能以计算所需的所有东西。 它已经找到了单词的开始和结束位置,因此,简单的单词计数很容易实现。 还有字长。 等等。

1)获取字符串,

2)使用strtok()将每个单词分隔为空格。

3)逐个字符逐个循环遍历每个字符串,以检查其是否为元音。

请检查以下代码

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

    int count_vowels(char []);
    int check_vowel(char);

    main()
    {
      char array[100];
      printf("Enter a string\n");
      gets(array);
      char seps[] = " ";
      char* token;
      int input[5];
      int i = 0;
      int c = 0;
      int count = 0;

      token = strtok (array, seps);
      while (token != NULL)
      {
         c = 0;
         c = count_vowels(token);
         if (c >= 3) {
            count++;
         }
         token = strtok (NULL, seps);
       }
       printf("Number of words that contain atleast 3 vowels : %d\n", count);
       return 0;
    }

    int count_vowels(char a[])
    {
       int count = 0, c = 0, flag;
       char d;

       do
       {   
          d = a[c];

          flag = check_vowel(d);

          if ( flag == 1 )
             count++;

          c++;
       }while( d != '\0' );

       return count;
    }

    int check_vowel(char a)
    {
       if ( a >= 'A' && a <= 'Z' )
          a = a + 'a' - 'A';   /* Converting to lower case */

       if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
          return 1;

       return 0;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM