ある文に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