✔ 最佳答案
You have the right approach to the problem. There were not a lot to fix-up.
There were a few errors with indices.
if (x[i] == vowel[j])
Also, you counted the number of spaces as words, which is not valid, since there could be multiple spaces between words. You need a status variable (isSpace) to indicate the end/beginning of word.
Keep up the good work.
#include
#include
#include
main()
{
char x[150];
int i, j, wcnt, frequency[5];
int isSpace=1;
char vowel[6] = {'a','e','i','o','u','\0'};
/* initialize variables */
wcnt = 0;
for (j = 0; j < 5; j++)
frequency[j] = 0;
/* Input a string */
printf("Please enter a string : ");
gets(x);
/* Determine number of words */
for(i = 0; i < strlen(x); i++){
if(isspace(x[i]))isSpace=1;
else {
if(isSpace==1)wcnt++;// to avoid adding word with multiple spaces
isSpace=0;
}
for(j = 0; j <5; j++){
if (x[i] == vowel[j]) frequency[j]+=1;
}
}
/* Outputting result */
printf("text:\n%s\n",x);
printf("Number of words = %d\n", wcnt);
for(j = 0; j <5; j++)
printf("frequency of %c = %d\n", vowel[j], frequency[j]);
//system("PAUSE");
}