改下面program改到一半就開始搞唔掂
程式碼:
http://www.hkedcity.net/sch_files/a/msc/msc-03cc5/public_html/HW_15_input.txt
佢要我o地找返晒所有errors出o黎同埋改正!
The sample output as follows:
Please enter a string:
Computer and information technolgy
Number of words = 4
Frequency of 'a' = 2
frequency of 'e' = 2
frequency of 'i' = 2
frequency of 'o' = 5
frequency of 'u' = 1
2006-11-26 18:46:10 · 1 個解答 · 發問者 John 2 in 電腦與網際網路 ➔ 程式設計
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");
}
2006-11-26 19:45:10 · answer #1 · answered by p 6 · 0⤊ 0⤋