K&R Exercise 1-9 (C)

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

    Considering what's asked in the question, I have also made sure that the program runs smooth in case of various tabs, spaces as well as when they're clubbed together to form various combinations! Here's my code,

    int c, flag = 1;
        printf("Enter the character!\n");
        while ((c = getchar()) != EOF) {
        if (c == ' '||c == '\t') {
            c=getchar();
            while(c == ' '|| c == '\t')
                   {
                       c = getchar();
                   }
            putchar(' ');
            if (c == EOF) break;
        }
        putchar(c);
    }
    

    Feel free to run all test cases using various combinations of spaces and tabs.

提交回复
热议问题