2
我正在編寫一個程序來查找給定句子中存在多少個單字母,雙字母,三字母,四個字母的單詞,並且我終於拿出一些代碼。但是,有一個問題。代碼已成功編譯,但是在運行時,程序將失敗並退出,結果不會發生。計算給定句子中構成單詞的字母
int main(void)
{
char *sentence = "aaaa bb ccc dddd eee";
int word[ 5 ] = { 0 };
int i, total = 0;
// scanning sentence
for(i = 0; *(sentence + i) != '\0'; i++){
total = 0;
// counting letters in the current word
for(; *(sentence + i) != ' '; i++){
total++;
} // end inner for
// update the current array
word[ total ]++;
} // end outer for
// display results
for(i = 1; i < 5; i++){
printf("%d-letter: %d\n", i, word[ i ]);
}
system("PAUSE");
return 0;
} // end main