주어진 문장에 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