Exercise 1-20: Write the program detab, replace the tabs in the input with the appropriate number of spaces, so that the space is filled to the next tab stop bit place. Assuming that the tab terminator position is fixed, such as a stop bit appears every n columns.
Here you will understand "tabs" and "tab stop bits". The function of tab is to move the cursor to the next tab stop bit. For example, suppose the tab stop bit is 4, 8, 12, 16 ... already entered 10 characters, and then press TAB key, then the cursor will move to position 12, students create a new text document to try to understand.
The code is as follows:
1#include <stdio.h>2 3 #defineTABSTOP 8//in the console, the tab stop bit is generally 8,16,24,32 ...4 5 intMain ()6 {7 intTotal =0;//the total number of characters that this line outputs8 CharC//the currently read character9 while((c = GetChar ())! =EOF)Ten { One if(c = ='\ t')//if the read character is a tab A { - inttemp = Total/tabstop;//calculates the number of characters the output has occupied so far tabstop - intNextlocation = (++temp) * TABSTOP;//next tab stop bit the intNumofspace = Nextlocation-total;//how many spaces to output - for(intI=1; i<=numofspace;++i) - { -Putchar (' '); +++Total ; - } + } A Else if(c = ='\ n') at { - Putchar (c); -Total =0; - } - Else - { in Putchar (c); -++Total ; to } + } -}
"C programming Language" reading notes----exercise 1-20