K&R Exercise 1-9 (C)

后端 未结 30 1164
北荒
北荒 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:24

    Same explanation with Matt Joiner's, but this code does not use break.

    int c;
    
    while ((c = getchar()) != EOF)
    {
        if (c == ' ') /* find a blank */
        {
            putchar(' '); /* print the first blank */
            while ((c = getchar()) == ' ') /* look for succeeding blanks then… */
                ; /* do nothing */
        }
    
        if (c != EOF) /* We might get an EOF from the inner while-loop above */
            putchar(c);
    }
    

提交回复
热议问题