K&R Exercise 1-9 (C)

后端 未结 30 1117
北荒
北荒 2021-01-31 19:46

\"Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.\"

I\'m assuming by thi

30条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 20:26

    a way to make it easier for the new people are stuck on this book (by not knowing any thing then what brought up until page 22 in K&R).

    credits to @Michael , @Mat and @Matthew to help me to understand

    #include 
    main()
    {
     int c;
    
    while ((c = getchar()) != EOF) /* state of "an input is not EOF" (1) */ 
     {
            if (c == ' ') /* "blank has found" -> change the rule now */
            {
              while ((c = getchar ()) == ' '); /* as long you see blanks just print for it a blank until rule is broken (2) */
              putchar(' ');
            }
       putchar(c); /* either state (2) was broken or in state (1) no blanks has been found */
      }
    }
    

提交回复
热议问题