<pre name="code" class="cpp"><pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
void fun(char* str) //删除str中头尾空格和中间连续的空格
{
assert(str!=NULL);
int i=0,j=0;
while(str[j]==' ')
j++;
int len=strlen(str)-1;
while(str[len]==' ')
len--;
str[len+1]='\0';
while(str[j]!='\0')
{
while(str[j]==' ')
j++;
if(j>=2&&(str[j-1]==' '&&str[j-2]==' ')&&i)
str[i++]=' ';
else if(j>=2&&str[j-1]==' '&&str[j-2]!=' ')
str[i++]=' ';
str[i++]=str[j++];
}
str[i]='\0';
}
int main()
{
char str[]=" sd sdasd wed ";
fun(str);
printf("%s\n",str);
return 0;
}
去除字符串中的首尾空格并将连续的空格变为一个
最新推荐文章于 2022-04-21 10:53:48 发布