Exercise 1-20 Write the program detab, replace the tabs in the input with the appropriate number of spaces, so that the space fills the next tab stop bit. Assume that the position of the tab stop bit is fixed, such as a tab stop bit appears every n columns. Should n be a variable or a symbolic constant?
The code is as follows:
#include <stdio.h>//contains information about the standard library. #defineMAXLINE 1000//the maximum number of characters per line is 1000. #defineDetab 8//a tab is equal to 8 spaces, it is recommended to use symbolic constants to facilitate program modification. intGetlineCharLine[],intmaxline);voidReverseCharLine[],intlen);intMain ()//defines a function named Main, which does not accept parameter values. { intLen; CharLine[maxline]; while(len = getline (line, MAXLINE)) >0) {printf ("%s\n", line); } getchar (); //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. }intGetlineCharS[],intLim) { intc, I; for(i =0; I < Lim-1&& (c = GetChar ())! = EOF && c! ='\ n';) { if(c = ='\ t') { //If you enter a tab, the following algorithm is used to calculate how many spaces are required to fill. intj =0; J= detab-i%Detab; while(J >0) {s[i++] =' '; J--; } } Else if(c = ='\ n') {s[i++] =C; } Elses[i++] =C; } S[i]=' /'; returni;}
Personal Understanding:
Apply n as a symbolic constant to the global benefit of uniform modification of the program, with the number of spaces to replace the tab is always fixed.
C Programming language Exercises 1-20