我正在尝试编写一个程序来查找给定句子中存在多少个 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