#include
int main()
{
char cx,front='\0';
while(cx=getchar()!='\n')
{
if(cx!=' ')putchar(cx);
if(cx==' ')
if(cx!=front)
putchar(cx);
front=cx;
}
return 0;
上面程序接受键盘上的输入,直到按enter键为止,这些字符被原样输出,但若有连续的一个以上的空格时只输出一个空格
还是建议规范化一下代码,否则不太容易读,不知你要的是不是下面实现的功能
int main()
{
char cx,front='\0';
while((cx=getchar())!='\n') //注意先getchar再比较是不是'\n'
{
if(cx!=' ')
{
putchar(cx);
front='\0';
}
if(cx==' ')
{
if(cx!=front)
{ putchar(cx);
front=cx;
}
}
}
return 0;
}
'\0'表示字符串结束。这代码看不出什么实际的用途,你得先说明你要做什么,才好知道有什么问题
while(cx=getchar()!='\n')
->
while((cx=getchar())!='\n')
'\0' 指字符串结束标志
'\0'表示字符串结束