(十萬火急)c++改program(string)

2006-11-27 7:46 am
改下面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

回答 (1)

2006-11-27 8:45 am
✔ 最佳答案
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");
}


收錄日期: 2021-04-12 20:08:44
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061126000051KK06781

檢視 Wayback Machine 備份