The 1-20 Detab of C programming exercises

Source: Internet
Author: User

Yard farming must be one of the most hands-on industries. There is a word called, good memory than rotten pen, far-fetched extension to here, become reading times, as the code word run again. Is that a little bit of a taste? Ha ha!

These days to see the "C Program design", read each chapter or feel not fully implemented, so honestly hands-on exercise to do it again, is not a practical heart of a confession. Carefully done to find that the problem is not the author of the simple Review summary of the text, but on the basis of a different and extended, if you miss, learn a lot less. Needless to say, the following is said 1-20 Detab this little exercise;

The topic is this:

Write the program detab, replace the tabs in the input with the appropriate number of spaces, so that the spaces are filled
Place where the next tab stop bit. Assume that the position of the tab Terminator is fixed, for example, every B-column will appear with a
The tab stop bit. "Should it be a symbolic constant as a variable?"

Read it two times and write the following code according to the understanding:

1#include <stdio.h>2 3 #defineTabnum 44 5 intMain ()6 {7     Chartext[ +];8     CharC;9     intindex =0;Ten  One      while((c = GetChar ())! =EOF) A     { -         if(c = ='\ t') -         { the              for(inti =0; i < tabnum; ++i) -             { -Text[index] =' '; -++index; +             } -         } +         Else A         { atText[index] =C; -++index; -         } -  -     } -  inprintf"%s\n", text); -  to     return 0; +}

CC compiles normally, when running verification, found that directly with 4 space substitution and input does not match, the input tab can be based on the actual input alignment

There should be some regularity in the number of spaces in the middle of the feeling. So, the Internet search really found that tab processing has a mystery. For example, the tab length is 8, starting from the beginning, input less than 8 characters, at this point, press TAB, the system will automatically move the cursor to the 9th character, the next character from now on, if you enter 9 characters at this time, the system will position the cursor to the 25th character, where the law can be summed up as:

pos = pos + 8-(pos-1)% 8

That is, when the cursor is in POS position, enter tab, the cursor jumps to POS + 8-(pos-1)% 8 position.

So the improved code is as follows:

1#include <stdio.h>2 3 #defineTabnum 84 5 intMain ()6 {7     Chartext[ +];8     CharC;9     intindex =0;Ten     intpos =1; One     intSpace_number; A  -      while((c = GetChar ())! =EOF) -     { the         if(c = ='\ t') -         { -Space_number = tabnum-(POS-1) %tabnum; -              while(Space_number >0) +             { -Text[index] =' '; +++index; A++Pos; at--Space_number; -             } -         } -         Else if(c = ='\ n') -         { -Text[index] =C; inpos =1; -++index; to         } +         Else -         { theText[index] =C; *++index; $++Pos;Panax Notoginseng         } -  the     } +  Aprintf"%s\n", text); the  +     return 0; -}

Run Validation:

perfect! Meet expectations.

The final summary is that the exercise is not just purely for the volume of the book thickening, or there is a real need to exercise! The important thing is to say this again.

The 1-20 Detab of C programming exercises

Related Article

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.