#include<stdio.h>
void RemoveEmpty(char *a)
{
bool flag=false;//true表示没有遇到空格
int newly=0;//标记新的字符串结尾处
if(a==NULL)
{
return;
}
//跳过开始的空格,去除中间多余的空格
for(int i=0;a[i]!=NULL;i++)
{
if(a[i]!=' ')
{
a[newly++]=a[i];
flag=true;
}
else if(flag)
{
a[newly++]=a[i];
flag=false;
}
}
//处理结尾的空格
if(a[newly-1]==' ')
{
a[newly-1]='\0';
}
else
{
a[newly]='\0';
}
}
int main()
{
char a[]=" a bc d ";
printf(a);
printf("\n去除多余空格后为:");
RemoveEmpty(a);
printf(a);
printf("\n");
return 0;
}
去除字符串多余的空格
最新推荐文章于 2023-03-17 09:55:52 发布