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

前端 未结 5 2097
無奈伤痛
無奈伤痛 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: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
    }

提交回复
热议问题