问题:编写一个程序,使用
以下是我编写的伪代码:
getchar()
接收文本输入并输出字符串,已去除多余的空格。以下是我编写的伪代码:
While each input character is received before reaching EOF, do the following:
1) if character is non-blank, print it out
2) otherwise:
a. print out the blank
b. do nothing untill the next non-blank character
3) if a non-blank character is reached, go back to 1)
我尝试按照以下方式实现算法:
#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
char c;
while((c= getchar())!=EOF){
if (c != ' ')
putchar(c);
else {
putchar(c);
while(c == ' ')
;
}
}
}
当一个字符串包含空格时,它会停止运行。我不确定应该如何调试它。我认为问题出在我的第二个while
循环中,程序陷入了死循环,而不是等待新的字符。
while(c == ' ') ;
是一个无限循环。更新c
。 - BLUEPIXY