1. 程式人生 > >C++去掉字串中重複空格

C++去掉字串中重複空格

#include<iostream>
#include<string.h>
using namespace std;
void delSpace(char* buf)
{
//int len = strlen(buf);
//int i,j;
//if(i = 0; i < len;i++)
char* fast = buf;
char* temp = buf;
while(*fast!='\0')
{
if(*fast!=' ')
{
*temp=*fast;
temp++;
fast++;
}
else
{
while(*fast == ' ')
fast++;
//cout<<*fast;
*temp=' ';
temp++;
}
}
*temp='\0';
}

int main()

{

char s[100];
int i = 0;
cin.getline(s,100);
delSpace(s);
i=0;
while(1)
{
if(s[i]=='\0')
break;
else
cout<<s[i];
i++;
}
return 0;

}