C语言删除句子中多余空格

#include<stdio.h>
#include<stdlib.h>
#define M 100
int main()
{
    int i,j=0,k;
    char a[M],b[M];
    scanf("%[^\n]",a);
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]!=' ')
        {
            b[j]=a[i];
            if(a[i+1]==' ')
            {
                b[j+1]=' ';
                j+=2;
            }
            if(a[i+1]!=' ')
            {
                j++;
            }
        }
    }
    b[j]='\0';
    printf("%s\n",b);
    system("pause");

    return 0;

}

下面是示例:

猜你喜欢

转载自blog.csdn.net/Poppy991122/article/details/79991140