K&R Exercise 1-9 (C)

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

    Pseudo code

    while c = getchar:
        if c is blank:
            c = getchar until c is not blank
            print blank
        print c
    

    C

    You can substitute use of isblank here if you desire. It is unspecified what characters contrive blank, or what blank value is to be printed in place of others.

    After many points made by Matthew in the comments below, this version, and the one containing isblank are the same.

    int c;
    while ((c = getchar()) != EOF) {
        if (c == ' ') {
            while ((c = getchar()) == ' ');
            putchar(' ');
            if (c == EOF) break;
        }
        putchar(c);
    }
    

提交回复
热议问题