到底怎么去除空白格!小white哭泣

我不明白我不明白!如图,要去除字符串中的空白符,我写的代码有问题吗??可以测试的时候发现就是在去空白符的时候出错了!

img

img

#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;
}

img