K&R Exercise 1-9 (C)

后端 未结 30 1149
北荒
北荒 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

    Using the constraints of not using else or and operators. This code only prints a blank when the blank variable is equal to 1 and the only way to reset the counter is by typing something other than a blank. Hope this helps:

    include

    /* Write a program that replaces strings of blanks with a single blank */

    void main(){ int c, bl;

    bl = 0;
    
    while((c = getchar()) != EOF){
        if(c == ' '){
            ++bl;
            if(bl == 1){
                putchar(' ');
            }
        }
        if(c != ' '){
            putchar(c);
            bl = 0;
        }
    }       
    

    }

提交回复
热议问题