Xx_Introduction

  • Character input and output is by more line character conpose of the text flow 
  • Define name common use capital  letter,easy read.
  • The Standard C Library ----->provide I/O model ------>use character flow way.

Ax_Application

  • file copy,charater count,line count,word count

Bx_Method

  • I/O model common use getchar and putchar,interactive use of putchar and printf.
    1 getchar()     //read next character
    2 putcahr()     //print next character
    3 printf()        //print next(bunch) character
  • File Copy
    • file copy version 1
       1 #include<stdio.h>
       2 
       3 int main()
       4 {
       5     int c;
       6     
       7     c = getchar();
       8     while(c != EOF){
       9         putchar(c);
      10         c = getchar();
      11     }
      12 }
    • file copy version 2
       1 #include<stdio.h>
       2 
       3 int main()
       4 {
       5     int c;
       6     
       7     while((c = getchar())!= EOF){
       8         putchar(c);
       9     }
      10 }

      != : unequal to. priority overtop assignment(=)             EOF:end of file

    • Conclusion:computer use bit storage of character and any data type.
    • Assignment can portion of expression.
    • Complex statement simple easy read,but so hard understand.
    • Due to unequal to relational operator(!=) priority not overtop assignment(=),so c expression use bracket.
       1 #include <stdio.h>
       2 
       3 int main()
       4 {
       5     int c;
       6 
       7     while (c = getchar() != EOF){
       8         printf("%d\n",c);
       9     }
      10     printf("%d - at EOF\n",c);
      11     return 0;
      12 }

      if not use bracket,will priority operation EOF,value by 1,if input end or else print "c - at EOF".

    • Print EOF value programming
      1 #include <stdio.h>
      2 
      3 int main()
      4 {
      5     printf("EOF is %d\n",EOF);
      6     return 0;
      7 }

      character constant EOF is in <stdio.h> file definition,value by -1 

    • In other system can by definition other value. 
  • Charater Count(2019-10-8 update)
    • Character count version 1
       1 #include <stdio.h>
       2 
       3 int main()
       4 {
       5     // version 1
       6     long nc;
       7 
       8     nc = 0;
       9     while (getchar() != EOF)
      10         ++nc;
      11     printf("%ld\n",nc-1);
      12     return 0;
      13 }

      ++(--) is operater, be euivalent to eg(nc = nc + 1);impression add one.

    • ++(--) use a prefix effect in variable before add.in suffix it's first call variable before use progressive increase.
    • Long than int more big,long support 32bit int support 16bit,but different system imparity,long in printf use %ld.
    • Character version 2
      1 #include <stdio.h>
      2 int main()
      3 {
      4     double nc;
      5     for (nc = 0; getchar() != EOF;++nc)
      6     ;
      7     printf("%.0f\n", nc-1);
      8     return 0;
      9 }

      double and float use %f format output.double more than float.

    • For circulation a circulation body must exsit,null statement for alone semicolon(;).
    • Important is circulation in execute programs before must judge test condition whether it meets. 
  • Line Count
    • mind:line equal to line break number.
    • Line count program
       1 #include <stdio.h>
       2 int main()
       3 {
       4     int c,nl;
       5 
       6     nl = 0;
       7     while ((c = getchar()) != EOF)
       8         if (c == '\n')
       9             ++nl;
      10     printf("%d\n",nl);
      11     return 0;
      12 }

       ==:mean equal.      'A':mean character constant.corresponding ASCII number.

  • Count blanks,tabs,and newlines.
    •  

      version 1

    •  1 #include <stdio.h>
       2 int main()
       3 {
       4     int c, nb, nt, nl;
       5 
       6     nb = 0;     /* number of blanks     */
       7     nt = 0;     /* number of tabs       */
       8     nl = 0;     /* number of newlines   */
       9     while ((c = getchar()) != EOF){
      10         if ( c == ' ')
      11             ++nb;
      12         if ( c == '\t')
      13             ++nt;
      14         if ( c == '\n')
      15             ++nl;
      16     }
      17     printf("blanks:%6d\ntabs:%6d\nnewlines:%6d\n", nb, nt, nl);
      18     return 0;
      19 }

      ....>>>

    • version 2
       1 #include <stdio.h>
       2 int main()
       3 {
       4     int c, nb, nt, nl;
       5 
       6     nb = 0;     /* number of blanks     */
       7     nt = 0;     /* number of tabs       */
       8     nl = 0;     /* number of newlines   */
       9     while ((c = getchar()) != EOF)
      10         if ( c == ' ')
      11             ++nb;
      12         else ( c == '\t')
      13             ++nt;
      14         else ( c == '\n')
      15             ++nl;
      16 
      17     printf("blanks:%6d\ntabs:%6d\nnewlines:%6d\n", nb, nt, nl);
      18     return 0;
      19 }
      View Code

      but I not  successful execute.

  • Replace string of blanks with a single blank
    • version 1
       1 #include <stdio.h>
       2 #define NONBLANK 'a'
       3 int main()
       4 {
       5     int c, lastc;
       6 
       7     lastc = NONBLANK;
       8     while (( c = getchar()) != EOF){
       9         if (c != ' ')
      10             putchar(c);
      11         if (c == ' ')
      12             if (lastc != ' ')
      13                 putchar(c);
      14         lastc = c;
      15     }
      16     return 0;
      17 }
      View Code

      one if statement control nonblank output.tow if statement deal with blank.three blank check blank is one or more blank.last print c.

    • version 2

       1 #include<stdio.h>
       2 #define NONBLANK 'a'
       3 int main()
       4 {
       5     int c, lastc;
       6 
       7     lastc = NONBLANK;
       8     while ((c = getchar()) != EOF ){
       9         if (c != ' ')
      10             putchar(c);
      11         else if (lastc != ' ')
      12             putchar(c);
      13         lastc = c;
      14     }
      15     return 0;
      16 }
      View Code

      ok,success!

    • version 3
       1 #include <stdio.h>
       2 
       3 #define NONBLANK 'a'
       4 int main()
       5 {
       6     int c, lastc;
       7 
       8     lastc = NONBLANK;
       9     while ((c = getchar()) != EOF){
      10         if (c != ' ' || lastc != ' ')
      11             putchar(c);
      12         lastc = c;
      13     }
      14     return 0;
      15 }
      View Code

      this method use logic simbol (OR) || realize.

    •  ok,this aim at blank deal wilth three method.

  • Replace tabs and backspaces with visible characters.
    • Realize
       1 #include<stdio.h>
       2 int main()
       3 {
       4     int c;
       5 
       6     while ((c = getchar())!= EOF){
       7         if (c == '\t')
       8             printf("\\t");
       9         if (c == '\b')
      10             printf("\\b");
      11         if (c == '\\')
      12             printf("\\\\");
      13         if (c != '\b')
      14             if (c != '\t')
      15                 if (c != '\\')
      16                     putchar(c);
      17     }
      18     return 0;
      19 }
      View Code

       

       why?//??????????????????????------------------------------program bug.......I brain in the blue screen.so,go to www.baidu.com to find out.

    • Truth  to version 2
       1 #include<stdio.h>
       2 int main()
       3 {
       4     int c;
       5 
       6     while ((c = getch())!= EOF){              /*getchar not identify keyboard backspace.*/
       7         if (c == '\t')
       8             printf("\\t");
       9         if (c == '\b')
      10             printf("\\b");
      11         if (c == '\\')
      12             printf("\\\\");
      13         if (c != '\b')
      14             if (c != '\t')
      15                 if (c != '\\')
      16                     putchar(c);
      17     }
      18     return 0;
      19 }
      View Code

      getchar not catch backspace so,will getchar replace getch. getch() can catch any print behavior.

    •  oh,yes!

    • It can also be used if-else. to version 3
       1 #include <stdio.h>
       2 int main()
       3 {
       4     int c;
       5 
       6     while((c = getch())!= EOF)
       7         if (c == '\t')
       8             printf("\\t");
       9         else if (c == '\b')
      10             printf("\\b");
      11         else if (c == '\\')
      12             printf("\\\\");
      13         else
      14             putchar(c);
      15     return 0;
      16 }
      View Code

      ok.next is a word count.

  • word count
    • count input lines,words and strings number.
       1 #include <stdio.h>
       2 #define IN 1
       3 #define OUT 0
       4 
       5 int main()
       6 {
       7     int c, nl, nw, nc, state;
       8 
       9     state = OUT;
      10     nl = nw = nc = 0;
      11     while ((c = getchar())!= EOF){
      12         ++nc;
      13         if (c == '\n')
      14             ++nl;
      15         if (c == ' ' || c == '\n' || c == '\t')
      16             state = OUT;
      17         else if (state == OUT){
      18             state = IN;
      19             ++nw;
      20         }
      21     }
      22     printf("lines:%9d\nword:%9d\nstring:%9d\n",nl,nw,nc);
      23     return 0;
      24 }
      View Code

      &&:AND  ||: OR , AND higher priority OR. expression from left to right.         a = b = c = 0   if meanwhile include value and assignment two type,order from right to left.

    • IF statement
      1 if(expression)
      2     statement1       /* true */
      3 else
      4     statement2       /* false */
      5 else if (expression){
      6     statement1       /* true*/
      7     ...
      8 }

      true or false.

  • Print input one word per line.
    • last practical program.very basis.very important.
       1 #include<stdio.h>
       2 
       3 #define IN 1
       4 #define OUT 0
       5 
       6 int main()
       7 {
       8     int c, state;
       9 
      10     state = OUT;
      11     while ((c = getchar()) != EOF){
      12         if (c == ' ' || c == '\n' || c == '\t'){
      13             if (state == IN){
      14                 putchar('\n');
      15                 state = OUT;
      16             }
      17         } else if (state == OUT){
      18             state = IN;
      19             putchar(c);
      20         } else
      21             putchar(c);
      22     }
      23     return 0;
      24 }
      View Code

      state is a BOOL value.

 

 

Cx_Conclusion

  1. I/O model common use getchar and putchar,interactive use of putchar and printf.
  2. File Copy
  3. Charater Count
  4. Line Count
  5. Count blanks,tabs,and newlines.
  6. Replace string of blanks with a single blank
  7. Replace tabs and backspaces with visible characters.
  8. word count
  9. Print input one word per line.
  10. getchar and putchar printf().and putch()
  11. != and ==\=
  12. ++ / -- 
  13. EOF
  14. LONG and DOUBLE
  15. while and if else
  16. ASCII 'A'
  17. || OR  ; && AND