1th Chapter-Introduction-Exercise 1.8-1.12

Source: Internet
Author: User

The previous exercise is relatively simple, according to the textbook can be done, the following records from exercise 1.8 began.

1.8

1#include <stdio.h>2 3 /*16/6/8 Count Blanks,tabs,and newlines*/4 Main ()5 {6     intC, Num_blank, Num_tab, num_new;7 8Num_blank = Num_tab = Num_new =0;9      while((c = GetChar ())! =EOF) {Ten         if(c = =' ') num_blank++; One         Else if(c = ='\ t') num_tab++; A         Else if(c = ='\ n') num_new++; -     } -printf" %d%d%d", Num_blank, Num_tab, num_new); the}
1-8.c

Initializing the statement is necessary, otherwise pressing ENTER after entering the statement will cause an error.

I used to write C51 microcontroller program in C, for the initial value of 0 variables are usually directly set the variable name is done (the C51 compiler will default to 0), this also gives me a reminder: For a newly defined variable, if necessary, it is best to add the initial value.

1.9

My idea is very simple, first read a character by C=getchar (), if it is a continuous space, then read into the next non-whitespace character, not the word directly print the character.

1#include <stdio.h>2 3 /*16/6/8 repacing string of blanks with a single blank*/4 Main ()5 {6     intC;7     8      while((c = GetChar ())! =EOF) {9         if(c = =' ') {Ten              while((c = GetChar ()) = =' '); OnePutchar (' '); A Putchar (c); -         } -         ElsePutchar (c); the          -     } -}
1-9.c

The answer is given in three ways, and the highlight is to set the integer variable LASTC to record the previous input character, and initially initialize it to the symbol constant nonblank (which can be set to any non-whitespace character by define). Here's a third way to write the answer (a little harder to understand)

1#include <stdio.h>2 #defineNonBlank ' a '3 4 /*16/6/15 repacing string of blanks with a single blank*/5 Main ()6 {7     intC;8     intLASTC;9 TenLASTC =nonblank; One      while((c = GetChar ())! =EOF) { A         if(c! =' '|| LASTC! =' ') - Putchar (c); -LASTC =C; the     } -}
1-9-A.C

1-10

1#include <stdio.h>2 3 /*16/6/8 Replace Tab,backspace,backslash with the \* form*/4 Main ()5 {6     intC;7 8      while((c = GetChar ())! =EOF) {9         //if (c = = ' \ t ') Putchar (' \\t ');Ten         if(c = ='\ t') printf ("\\t"); One         Else if(c = ='\b') printf ("\\b"); A         Else if(c = ='\\') printf ("\\\\"); -         ElsePutchar (c); -     } the}
1-10.c

Just started using a similar putchar (' \\t ') Form, the output is as follows

Replace with printf to achieve the desired effect, note that in the C language, the backslash is represented by ' \ \ ', in order to output two backslashes, you need to use the printf output string "\\\\" (should be the front \ \ output a \, then the back \ \ and then output a \).

1-11

To see the answer, the key is to satisfy the input of the boundary conditions, such as no input, no words (only spaces, tabs, or newline characters), one row per word (no spaces and tabs), and the word appears after a string of spaces.

1-12

On the basis of the textbook P14, the idea is that the current reading of the character in the word (state=in) printed out, until the next Space/tab/carriage return, and print a return letter \ n another line.

1#include <stdio.h>2 3 #defineIn 14 #defineOut 05 6 /*16/6/8 output in the form of a single word per line*/7 Main ()8 {9     CharState =Out ;Ten     CharC; One  A      while((c = GetChar ())! =EOF) { -         if(c = =' '|| c = ='\ t'|| c = ='\ n') { -State =Out ; the         } -         Else if(state = =Out ) { -State =in ; -         } +          while(state = =In ) { - Putchar (c); +c =GetChar (); A                 if(c = =' '|| c = ='\ t'|| c = ='\ n') { atState =Out ; -Putchar ('\ n'); -                 } -         } -     } -}
1-12.c

The answer to write better, here put the source code to understand.

1#include <stdio.h>2 3 #defineIn 14 #defineOut 05 6 /*16/6/15 output in the form of a single word per line*/7 Main ()8 {9     CharState =Out ;Ten     CharC; One  A      while((c = GetChar ())! =EOF) { -         if(c = =' '|| c = ='\ t'|| c = ='\ n') { -             if(state = =In ) { thePutchar ('\ n'); -State =Out ; -             } -         } +         Else if(state = =Out ) { -State =in ; + Putchar (c); A         } at         ElsePutchar (c); -     } -}
1-12-A.C

1th Chapter-Introduction-Exercise 1.8-1.12

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.