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

前端 未结 5 2079
無奈伤痛
無奈伤痛 2021-01-22 20:47

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 o

相关标签:
5条回答
  • 2021-01-22 21:11

    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.

    0 讨论(0)
  • 2021-01-22 21:12

    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;
    }
    
    0 讨论(0)
  • 2021-01-22 21:26

    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
    }

    0 讨论(0)
  • 2021-01-22 21:31

    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]);
    
    0 讨论(0)
  • 2021-01-22 21:32

    (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;
    }
    
    0 讨论(0)
提交回复
热议问题