1文字、2文字、3文字、4文字の単語がいくつあるかを特定するプログラムを作成しようとしています。いくつかのコードを思いついてください。しかし、問題があります。コードは正常にコンパイルされましたが、実行するとプログラムは失敗し、結果がなくなります。与えられた文章中の単語を構成する文字を数える
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