C 问题, 急~~~请进
这是C 的问题, 很简单. 可是我花了两个小时, 还是做不来.
我只是想把多余的空格去掉, 如:
"This is a boy." to "This is a boy" etc.
请救救我吧. 告诉我的CODE那里出错.
------------------------------------------
string case1 = " abc lol a b" ;
int length = case1.length() - 1;
int length2 = case1.length();
int i = 0;
while(length != 0) {
if (case1[i 1] == length2) break;
if(case1[i] == ' ' && case1[i 1] == ' ') {
case1.erase(i, 1);
i--;
length --;
}
i ;
}
解决方法
string case2="";
for(int i=0 ; i<case1.length() ; i )
{
if(case1[i]!=' ')
{
case2 =case1[i];
}
}
cout<<case2<<endl;
下面是C#代码,仅供参考。
string str="abc lol a b";
while(str.IndexOf(" ")!=-1) str=str.Replace(" "," ");