连续多个空格以一个空格代替

#include 
int main()
{
     
	char c = getchar(), pre = 'a';
	while(c != EOF)
	{
     
		if(c != ' ' || pre != ' ') putchar(c);
		pre = c;
		c = getchar();
	}
	return 0;
}

或者

#include
int main(){
     
	char s1[100],s2[100];
	int i=0,j=0,count=0;
	gets(s1);
	while(s1[i]!='\0'){
     
		if(count>=1&&s1[i]!=' '){
     
			count=0;
		}
		if(s1[i]==' ')count++;
		if(count<=1){
     
			s2[j]=s1[i];
			j++;
		}
		i++;
	}
	s2[j]='\0';
	puts(s2);
	return 0;
}

再或者

#include
int main(void)
{
     
    char s1[100],s2[100]="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";int i,j=0;
    gets(s1);
    for(i=0;s1[i]!='\0';i++)
    {
     if(s1[i]!=' ')
    s2[j]=s1[i],j++;
    if(s1[i]==' ')
    if(s1[i-1]!=' ')
    s2[j]=s1[i],j++;
}
s2[j]='\0';
    printf("%s\n",s2);

return 0;
}

还有

#include 
#include 
int main()
{
     
int flag=0, i, j=0;
char s1[1000],s2[1000];
gets(s1);
int n=strlen(s1);
for(i=0;i<n;i++){
     
    if(s1[i]==' '){
     
    s2[j]=s1[i];
    flag=1;    
    }
    if(s1[i]!=' '){
     
    s2[j]=s1[i];
    flag=0;    
    }
    if(flag==1&&s1[i-1]==' '){
     
    j--;
    }
    j++;
}
s2[j]='\0';
puts(s2);
return 0;
}

你可能感兴趣的:(C)