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

Source: Internet
Author: User

The title is not written, probably means: try to use the tab ' \ T ' to replace the space in the string.

Students need to note that printing a tab ' \ t ', the length of the account is not fixed.

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 topic seems simple, but it takes a bit of brains to write a simple, clear procedure.

/*This program may seem simple, but it takes a little bit of brains to write a simple, clear program. To think: Under what circumstances can you use a ' \ t ' instead of a space? In fact, there is only one situation, that is: when a "non-whitespace character" and the character's next tab terminator is all spaces between the time, you can use a tab ' \ t ' to replace these spaces for example: (with _ for a space, with | Indicates a tab stop bit, note | Not part of the string) _ _ A B C _ _ | _ b c D f t q u r | _ D can be replaced by a tab space only: (with the * number to replace the space with a tab) _ _ A b c * * | _ b c D f t q u r | _ D*/#include<stdio.h>#defineTABSTOP 8//in the console, the tab stop bit is generally 8,16,24,32 ...intMain () {intTotal =0;//number of characters that have been read    intSpaces =0;//number of spaces that have been read    CharC//the currently read character     while((c = GetChar ())! =EOF) {        if(c! =' ')//if C is not a space        {              if(Spaces = =0)//If you have read 0 spaces{Putchar (c); ++Total ; }            Else   //c is not a space, but the number of spaces read is not equal to 0            {                 for(; spaces >0; --spaces)//Output SpacesPutchar (' '); Spaces=0;//number of spaces read reset to 0++Total ; Putchar (c);//outputs the currently read characters            }            if(c = ='\ n') { Total=0; Spaces=0; }        }        Else //read a space        {            ++Total ; ++spaces; inttemp = (total-spaces)/TABSTOP; intNextlocation = (++temp) * TABSTOP;//next tab stop bit            if(total<nextlocation); Else//fill space with ' \ t '            {                 for(inti =0; I < (Nextlocation-(total-spaces)); i++)//in order to clear the output is ' \ t ', with the * number is more intuitive. Putchar ('*'); //Putchar (' \ t ');Spaces =0;//number of spaces read reset to 0            }        }    }    return 0;}

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

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.