首页 > 解决方案 > 我的程序不计算元音。你能告诉我为什么吗?

问题描述

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

/*Variables are listed below*/ 
int main() {
    int i=0, j=0, k=0,a, minIndex=0,maxIndex=0,max=0, min=0;
    int c=0, count=0;
    int Vowel(char ch);
    char countries[100],lWords[100][100];

/*Words counting code*/
    printf("***WORD LENGTH AND VOWELS COUNTER PROGRAM***\n");
    printf(" Output the number of vowels in both words\n");
    printf("***ENTER 10 COUNTRIES FROM THE LIST BELOW AS INPUT:\n");
    printf(" ARGENTINA, AUSTRALIA, BRAZIL, CANADA, CHINA, FRANCE, GERMANY, INDIA, INDONESIA, ITALY,JAPAN/n");
    printf("REPUBLIC OF KOREA, MEXICO, RUSSIA, SAUDI ARABIA, SOUTH AFRICA, TURKEY, UK, USA\n");
    gets(countries);


    while(countries[k]!='\0')
    {
        j=0;
        while(countries[k]!=' '&&countries[k]!='\0')
        {
            lWords[i][j]=countries[k];
            k++;
            j++;
        }
        lWords[i][j]='\0';
        i++;
        if(countries[k]!='\0')
        {
            k++;
        }
    }

    int length=i;
    max=strlen(lWords[0]);
    min=strlen(lWords[0]);
    for(i=0; i<length; i++)
    {
       a=strlen(lWords[i]);
       if(a>max)
        {
            max=a;
            maxIndex=i;
        }
        if(a<min)
        {
            min=a;
            minIndex=i;
        }
    }
      while(countries[k]!='\0');
       {

   /* Vowels counting code is written below*/   
int  Vowel (char ch){
scanf("%c", &ch);
    if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
      count++;
    c++;
  }

printf("/n The largest word is: %s\n", lWords[maxIndex]);
printf("/n The number of vowels in the string '%s' is: %d\n", lWords[maxIndex],count);
printf("/n The smallest word is: %s\n",lWords[maxIndex]);
printf("/n The number of vowels in the string '%s' is: %d\n", lWords[minIndex],count);
       }
    return 0;
}

编写一个程序,从用户那里获取单词列表作为输入,并输出提供的最大和最小单词。您的解决方案还必须输出两个单词中元音的数量。从下面的列表中输入 10 个国家作为输入:阿根廷、澳大利亚、巴西、加拿大、中国、法国、德国、印度、印度尼西亚、意大利、日本、大韩民国、墨西哥、俄罗斯、沙特阿拉伯、南非、土耳其、英国、美国。

当我列出 10 个国家时,代码的第一部分可以正常工作,它可以识别最大和最小的国家。但是,它不计算单词中的元音,从而为“巴西”提供零元音而不是 2 个元音。我需要在代码中添加或更改什么?

标签: c

解决方案


主要问题是您从不计算元音。您为此 ( Vowel()) 编写了一个函数,但从未使用过它。无论如何,该函数包含不合适的语句c++......因为我们正在使用C. 我在开玩笑,这没有任何意义。甚至scanf不应该在那里,因为您将角色作为参数传递。

另外我建议你把原型和声明都Vowel()放在外面main()

也就是说,我给你一个函数来计算一个单词中的元音,你可以做一个类似的函数来检查一个字符是否是元音,或者检查给定的字符串是否与其中一个国家匹配。

int howManyVowels( char *s )
{
    static const char vowels[] = "aeiouAEIOU";
    int count = 0;
    
    for(int i = 0; s[i]; i++) if( strchr(vowels, s[i]) ) ++count;
    
    return count;
}

玩得开心。

编辑:“我用你的代码替换了上面的代码,但它仍然是一样的。另外,我把元音 decleration 放在了主代码之外。”

你不能只是把我的功能放在你的程序中。您的程序需要一个检查字符是否为元音的函数,而不是一个给出单词中元音数量的函数。我给你的功能作为一个例子来帮助你做你的。这是一个使用我的函数的示例程序:

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

#define SIZE 50

int howManyVowels( char *s );

int main() {

    char word[SIZE], longest[SIZE], shortest[SIZE];
    int len, max = 0, min = SIZE;
    
    while( scanf(" %s", word) == 1 ) {
        
        if( max < (len = strlen(word)) ) {
            
            max = len;
            strcpy(longest, word);
        }
        else if( min > len ) {
            
            min = len;
            strcpy(shortest, word);
        }
    }
    
    printf("The longest one was %s, which has %d vowels.\nThe shortest was %s, which has %d vowels.\n", longest, howManyVowels(longest), shortest, howManyVowels(shortest));

    return 0;
}

int howManyVowels( char *s )
{
    static const char vowels[] = "aeiouAEIOU";
    int count = 0;
    
    for(int i = 0; s[i]; i++) if( strchr(vowels, s[i]) ) ++count;
    
    return count;
}

但是,该程序不会检查该词是否为国家之一。我建议你以我的函数为例来构建你的程序需要的函数,即判断一个字符是否为元音的函数。您还可以以它为例来构建一个检查给定单词是否与其中一个国家匹配的函数。


推荐阅读