"C programming Language" reading notes----exercise 1-20

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.