去掉字符串多余的空格
程序员文章站
2024-01-20 17:26:34
...
#include<iostream>
using namespace std;
char *deblank(char *arr)
{
if(arr == NULL)
return NULL;
char *slow = arr;
char *fast = arr;
while(*fast == ' ' && *fast != '\0')
{
fast++;
}
while(*fast != '\0')
{
if(*fast != ' ' )
{
*slow++ = *fast++;
}
else
{
*slow++ = ' ';
*fast++;
while(*fast == ' ' && *fast != '\0')
{
fast++;
}
}
}
*slow = '\0';
return arr;
}
int main()
{
char arr[] = " I like you !";
printf("删除多余的空格前:\n%s\n", arr);
//char *p;
deblank(arr);
printf("删除多余的空格后:\n%s\n", arr);
return 0;
}