将输入复制到输出的程序,将连续的空格用一个空格代替。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int now,former;
while((now = getchar()) != EOF){
if (now != ' ')
putchar(now);
else{
former = now;
now = getchar();
if (now != ' '){
putchar(former);
putchar(now);
}
else
putchar(now);
}
}
system("pause");
return 0;
}
我写的这种方法只能将连续的两个空格用一个代替,有什么好办法呢
[解决办法]
#include <stdio.h>
#include <stdlib.h>
int main(void){
int now;
BOOL hasSpace = TRUE;
while((now = getchar()) != EOF){
if (now != ' '){
putchar(now);
hasSpace = FALSE;
}
else if(!hasSpace){
putchar(now);
hasSpace = TRUE;
}
}
system("pause");
return 0;
}