删除多余空格

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void delspace(char s[],char newstr[])
{
    int i;
    int count=0;
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]!=' ')
        {
            while((s[i]!=' ')&&(i<strlen(s)))
            {
                newstr[count++]=s[i++];
            }
            newstr[count++]=' ';
        }
    }
    newstr[count-1]='\0';
}

int main()
{
    char s[]="   I    LOVE YOU   ";
    char newstr[200];
    delspace(s,newstr);
    printf("%s\n",newstr);
    puts(newstr);
    for(int i=0;i<strlen(newstr);i++)
    {
        printf("%c",newstr[i]);
    }
}



在这里插入图片描述

发布了54 篇原创文章 · 获赞 1 · 访问量 513

猜你喜欢

转载自blog.csdn.net/weixin_43370733/article/details/103796003