我不明白我不明白!如图,要去除字符串中的空白符,我写的代码有问题吗??可以测试的时候发现就是在去空白符的时候出错了!
#include <iostream>
#include <string>
using namespace std;
string ClearAllSpace(string str)
{
int i = 0;
// 如果我没看错,你漏了 i++,导致无法进入内层循环了
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
for (int j = i; str[j] != '\0'; j++)
{
str[j] = str[j + 1];
}
}
}
return str;
}
int main()
{
std::cout << ClearAllSpace("aa bb b") << std::endl;
std::cin.get();
return 0;
}