What is wrong with this C program to count the occurences of each vowel?

左心房为你撑大大i 提交于 2019-12-02 04:56:19

问题


PROBLEM:

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:

“The brown fox jumped over the lazy dog”

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

Total number of Vowels= 10

MY 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.


回答1:


First of all you should put the return type for main: int ( ex: int main() ), this did not break your code but it the C standard and rises a warning from the compiler.

Chars in C take the numerical value from the ASCII encoding standard: http://upload.wikimedia.org/wikipedia/commons/1/1b/ASCII-Table-wide.svg, look at the values there ( 'a' is 97 for example), more on wikipedia : http://en.wikipedia.org/wiki/ASCII

All you do in your last for loop is compare the character to a,b,c,d,e.

What I would recommend you to do is make a switch for the character:

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

Alternatively, you could make five if statements.

It should work fine now.




回答2:


(just for fun, my answer)

int main(void)
{
    int counter[5] = {0};
    int *i, c;

    printf("Please enter a string terminated by ENTER key:\n");
    while(i = counter, (c = getchar()) != '\n')
    {
        switch(c)
        {
          default: continue;
          case 'U'+' ': case 'U': ++ i;
          case 'O'+' ': case 'O': ++ i;
          case 'I'+' ': case 'I': ++ i;
          case 'E'+' ': case 'E': ++ i;
          case 'A'+' ': case 'A': ++*i;
        }
    }

    for(c = 0; c < 5;

    printf("counter[%d] = %d\n", c, counter[c++]));

    return 0;
}



回答3:


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]);

OR using switch:

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]);



回答4:


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

I feel the logic in this code may be wrong because, you have initialized your counter[0] with 0 and then you are comparing it with 'a' or 'A' but not the char c, so use

while((c = getchar()) != '\n') { if(c == 'a' || c == 'A') { i = 0; counter[i] =+ 1; } else if( c == 'i' || c == 'I' ) { i = 1; counter[i] =+ 1; } ...//and so on
}




回答5:


Apparantly none of the other answerers so far have noticed these possibilities to improve the code:

  • Define the vowels as a string and write a function that returns the index of the vowel in that string (or -1 in case of a not-a-vowel error).
  • Convert the character to lowercase before doing the comparison. This is a very common approach to achieve case insensitive behavior.
  • You should also check for EOF as an exit condition for your loop.

I think, what you need is something like this:

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

const char vowels[] = "aeiou";

int findvowel(char c) {
    int i;
    c = tolower(c);
    for (i=0; vowels[i]; i++)
        if (vowels[i] == c)
            return i;
    return -1;
}

int main() {
    char c;
    int i, sum;
    int counter[sizeof(vowels)] = {0};
    while (c=getchar(), c != EOF && c != '\n') {
        i = findvowel(c);
        if (i != -1)
            counter[i] += 1;
    }
    printf("having found these vowels:\n");
    for (i=0, sum=0; vowels[i]; i++) {
        printf("'%c': %d\n", vowels[i], counter[i]);
        sum += counter[i];
    }
    printf("total: %d %s\n", sum, sum != 1 ? "vowels" : "vowel");
    return 0;
}


来源:https://stackoverflow.com/questions/22140125/what-is-wrong-with-this-c-program-to-count-the-occurences-of-each-vowel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!