Practice 1-11 How are you going to test the word count program? If there is some kind of error in the program, what kind of input is most likely to find such errors?
The code is as follows:
#include <stdio.h>//contains information about the standard library. #defineIn 1/* within the word */#defineOut 0/* outside the word */intMainvoid)//defines a function named Main, which does not accept parameter values. {printf ("====== counter Program ======\n"); intC, NL, NW, NC, State; State=Out ; NL= NW = NC =0; while((c = GetChar ())! =EOF) { //The character counter increments. ++NC; if(c = ='\ n') ++nl;//The line break counter increments. //If you encounter spaces, line breaks, and tabs, the status is recorded. //if the next character is not one of these three symbols, the word counter increments. if(c = =' '|| c = ='\ n'|| c = ='\ t') state =Out ; Else if(state = =Out ) { state=In ; ++NW; }} printf ("%d lines,%d words, total%d characters. \ n", NL, NW, NC); Getch (); //prevent the console from flashing through, you need to accept any characters after you close the console. return 0;//returns a shape to the execution environment, and 0 represents a successful execution. }
Personal Understanding:
In this program, the character counter will also count the newline characters ' \ n ' as a character, and the program will not terminate when the word multibyte EOF is entered directly. :
Typing the ENTER key does not terminate the program.
C Programming language Exercises 1-11