编写一个将输入复制到输出的程序,并将其中连续多个空格用一个空格代替
- #include<stdio.h>
- main()
- {
- int c,aa,bb,cc;
- int lastchar=0;
- int done=1;
- aa=bb=cc=0;
- while (done==1)
- {
- c=getchar();
- if (c==' ')
- {
- if (lastchar!=' ')
- printf(" ");
- ++bb;
- ++cc;
- }
- else if (c=='0')
- done=0;
- else
- putchar(c);
- lastchar=c;
- ++aa;
- }
- printf("aa%d bb%d cc%d/n",aa,bb,cc);
- }
这题还是给我想了点时间,可能我比较笨吧,呵呵。。。
答案:
#include <stdio.h>
int main(void)
{
int c;
int inspace;
inspace = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace == 0)
{
inspace = 1;
putchar(c);
}
}
/* We haven't met 'else' yet, so we have to be a little clumsy */
if(c != ' ')
{
inspace = 0;
putchar(c);
}
}
return 0;
}