【问题标题】:How to print strings that have more consonants than vowels?如何打印辅音多于元音的字符串?
【发布时间】:2026-01-07 01:25:14
【问题描述】:

输入 20 个字符串的数组。编写程序,打印出辅音比元音多且字母“r”至少重复 3 次的字符串。

我相信问题出在我的 if 循环中,但不知何故我不明白为什么它不能正常工作。它打印我输入的每个字符串。

这是我写的代码:

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


    int main(void) {

    char string[20][50];
    int i, j;
    int vowels=0;
    int consonants=0;
    int repeated_r=0;

    printf("Enter the array of 20 strings:\n");

    for(i=0;i<20;i++){
        gets(string[i]);
        }

    for(i=0;i<20;i++){
        for(j=0;j<50;j++){

            if(string[i][j] == 'r'){
                repeated_r++;
            }
            else if(string[i][j] == 'a' || string[i][j] == 'e' || string[i][j] == 'i' || string[i][j] == 'o' || string[i][j] == 'u'){
                vowels++;
            }
            else{
                consonants++;
            }

        }
        if(consonants>vowels && repeated_r>=3){

            fflush(stdin);
            puts(string[i]);
        }
    }


    return 0;
}    

【问题讨论】:

  • r 是一个辅音,所以如果你有 turreteer 这个词,它不会被打印出来,因为你会数 3 个r,4 个元音和 2 个辅音

标签: c arrays string for-loop char


【解决方案1】:

您需要在处理完每个字符串后重置计数器。

不要使用gets,而是使用fgets

for(i=0;i<20;i++){
    for(j=0;j<50;j++){

        if(string[i][j] == 'r'){
            repeated_r++;
        }
        else if(string[i][j] == 'a' || string[i][j] == 'e' || string[i][j] == 'i' || string[i][j] == 'o' || string[i][j] == 'u'){
            vowels++;
        }
        else{
            consonants++;
        }

    }
    if(consonants>vowels && repeated_r>3){

        fflush(stdin);
        puts(string[i]);
    }

   //Reset the counters
    consonants =0;
    vowels =0;
    repeated_r =0;
  }
 }

另请注意,在您当前的代码中,r 不被视为辅音。

【讨论】:

    【解决方案2】:

    您没有重置外循环中变量的初始值

    int vowels=0;
    int consonants=0;
    int repeated_r=0;
    

    也是内循环中的条件

    for(j=0;j<50;j++){
            ^^^^
    

    不正确,因为在这种情况下,可以访问数组中存储的字符串之外的内存。

    字母'r' 不算作辅音。

    考虑到函数 gets 不是标准 C 函数,因此 C 标准不再支持它。

    还有这个电话

    fflush(stdin);
    

    具有未定义的行为。

    我可以建议以下解决方案,如下面的演示程序所示。

    #include <stdio.h>
    #include <string.h>
    
    int main( void ) 
    {
        enum { M = 3, N = 50 };
        char s[M][N];
    
        for ( size_t i = 0; i < M; i++ )
        {
            fgets( s[i], N, stdin );
            s[i][strcspn( s[i], "\n" )] = '\0';
        }
    
        const char *vowels = "aeiou";
        const char r = 'r';
    
        for ( size_t i = 0; i < M; i++ )
        {
            size_t repeated_r = 0;
            size_t vowels_count = 0;
            size_t n = strlen( s[i] );
    
            for ( size_t j = 0; j < n; j++ )
            {
                repeated_r += s[i][j] == r;
                vowels_count += strchr( vowels, s[i][j] ) != NULL;
            }
    
            if ( repeated_r >= 3 && vowels_count < n - vowels_count )
            {
                puts( s[i] );
            }
        }
    
        return 0;
    }
    

    如果要输入以下字符串

        Hello World
        errors
        photosynthesis_bro
    

    那么程序输出可能看起来像

    errors
    

    【讨论】:

      【解决方案3】:

      您的代码中还有一个问题,您正在使用获取。它还会包含空格,所以如果你的字符串是“_ _ rrrbaeeeeiou _ ”。它会打印这个字符串,但实际上这个字符串不应该被打印出来。 请注意,“”表示空格。 根据您的代码,“_”将计入辅音,即使您的字符串中有 4 个辅音(3 r 和 1 b)和 8 个元音,它也会将此字符串打印为输出,因为空格将被计为辅音. 同样在您的代码中,r 不计为辅音

      【讨论】:

        最近更新 更多