Practice 1-8 Write a program that counts the number of spaces, tabs, and line breaks.
The code is as follows:
#include <stdio.h>//contains information about the standard library. intMain ()//defines a function named Main, which does not accept parameter values. {printf ("====== counts the number of spaces, tabs, and line breaks in the characters entered ======\n"); //characters entered, space-space Counter, tab counter, line break counter. intC, SL, TL, NL; SL= TL = NL =0; while((c = GetChar ())! =EOF) { if(c = =' ') ++SL; if(c = ='\ t') ++tl; if(c = ='\ n') ++NL; } printf ("The characters you enter have%d spaces, a tab with%d, and a newline character of%d. ", SL, TL, NL); 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:
The key to practicing while in conjunction with GetChar () is to understand the GetChar () function and to judge the character obtained by the GetChar () function, incrementing the counter.
C Programming language Exercises 1-8