什么是不对的C程序来计算每个元音的OCCURENCES? [英] What is wrong with this C program to count the occurences of each vowel?

查看:164
本文介绍了什么是不对的C程序来计算每个元音的OCCURENCES?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

编写提示用户输入由ENTER键终止字符的字符串(即'\\ n'),然后计算字符串中的每个元音的发生总数的C程序。你的程序应该使用以下准则:结果
•你的程序应该也就是柜台申报5整数计数器,一个数组。结果
•使用一个循环,你的程序应该在阵列中的每个成员初始化为0结果
•字符串中的每个字符,然后检查,看它是否是一个元音字母,即'一','E','我','O'或'U'。在这种情况下,相应的计数器被递增。例如,如果一个一个被读出,然后计数器[0]被递增,如果一个i被读出,然后计数器[2]递增,并依此类推。结果
•输入的字符可以是大写或小写。结果
•你的程序应该使用一个循环印出5计数器的内容。结果
•最后你的程序应该打印字符串中的元音字母的总数。结果
结果应该是这样的:
请您输入回车键结束的字符串:

Write a C program that prompts the user to enter a string of characters terminated by ENTER key (i.e. ‘\n’) and then count the total number of the occurrence of each vowel in the string. Your program should use the following guidelines:
• Your program should declare an array of 5 integer counters, namely counter.
• Using a loop, your program should initialize each member of the array to 0.
• Each character in the string is then checked to see if it is a vowel, i.e. ‘a’, ‘e’, ‘i’, ‘o’ or ‘u’. In that case, the corresponding counter is incremented. For example if an ‘a’ is read then counter[0] is incremented, if an ‘i’ is read then counter[2] is incremented and so on.
• The entered characters could be lower or upper case.
• Your program should use a loop to printout the contents of the 5 counters.
• Lastly your program should print the total number of vowels in the string.
The result should look like: Please enter a string terminated by ENTER key:

的棕色狐狸跳过了懒狗

counter[0] = 1
counter[1] = 4
counter[2] = 0
counter[3] = 4
counter[4] = 1

元音总数= 10

Total number of Vowels= 10

我的code:

#include <stdio.h>
main(){
int counter[5];
int c, i;
for(i = 0; i < 5; i++)
counter[i] = 0;
printf("Please enter a string terminated by ENTER key:\n");
while((c = getchar()) != '\n')
{
     if((counter[i] == 'a' || counter[i] == 'e' || counter[i] == 'i' || counter[i] ==
    'o' || counter[i] == 'u') ||(counter[i] == 'A' || counter[i] == 'E' || counter[i] == 
    'I' || counter[i] == 'O' || counter[i] == 'U'))
    {
        for(i = 0; i < 5; i++)
        {
            if(c == 'a' + i)
            counter[i] = counter[i] + 1;
        }
    }
}
for(i = 0; i < 5; i++)
    printf("counter[%d] = %d\n", i, counter[i]);
}

这有什么错我的柜台?
提前致谢。

What's wrong with my counter? Thanks in advance.

推荐答案

我觉得这就是你正在尝试做的:

I think this is what you are trying to do:

printf("Please enter a string terminated by ENTER key:\n");
while((c = getchar()) != '\n')
{
    if (c=='a' || c=='A')
        counter[0]++;
    else if (c=='e' || c=='E')
        counter[1]++;
    else if (c=='i' || c=='I')
        counter[2]++;
    else if (c=='o' || c=='O')
        counter[3]++;
    else if (c=='u' || c=='U')
        counter[4]++;
}
for(i = 0; i < 5; i++)
    printf("counter[%d] = %d\n", i, counter[i]);

使用开关:

printf("Please enter a string terminated by ENTER key:\n");
while((c = getchar()) != '\n')
{
    switch(c)
    {
        case 'a':
        case 'A': counter[0]++;
                  break;
        case 'e':
        case 'E': counter[1]++;
                  break;
        case 'i':
        case 'I': counter[2]++;
                  break;
        case 'o':
        case 'O': counter[3]++;
                  break;
        case 'u':
        case 'U': counter[4]++;
                  break;
        default: break;
    }
}
for(i = 0; i < 5; i++)
    printf("counter[%d] = %d\n", i, counter[i]);

这篇关于什么是不对的C程序来计算每个元音的OCCURENCES?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆