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

六月ゝ 毕业季﹏ 提交于 2019-12-02 01:39:46
eduardtm

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.

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

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

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
}

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