C++ 删除C+中单词之间的所有空格,但只有一个空格+;

C++ 删除C+中单词之间的所有空格,但只有一个空格+;,c++,C++,我需要删除单词之间的所有空格,只有一个空格。我遍历字符串并if(temp.back()=''&&c!='')在进入下一个单词之前检查最后一个空格。但它删除了所有的空白。有没有提示问题出在哪里 string removeWhiteSpace(string current) { string myNewString = ""; string temp = ""; for (char c : current) { temp.push_back(c); if (temp.

我需要删除单词之间的所有空格,只有一个空格。我遍历字符串并
if(temp.back()=''&&c!='')在进入下一个单词之前检查最后一个空格。但它删除了所有的空白。有没有提示问题出在哪里

string removeWhiteSpace(string current)
{
  string myNewString = "";
  string temp = "";
  for (char c : current)
  {
    temp.push_back(c);
    if (temp.back() == ' ' && c != ' ')
    {
      myNewString.push_back(' ');
    }
    if (c != ' ')
    {
      myNewString.push_back(c);
    }
  }
  return myNewString;
}

您可能想要删除额外的空白。这段代码会有帮助

string removeWhiteSpace(string current)
{
  string myNewString = "";
  string temp = "";
  int l=current.length();
  myNewString+=current[0];
  for(int i=1;i<l;i++){
      if(myNewString[myNewString.length()-1]==' ' && current[i]==' ')
          continue;
      else
          myNewString+=current[i];
  }
  return myNewString;
}
string removeWhiteSpace(当前字符串)
{
字符串myNewString=“”;
字符串temp=“”;
int l=当前的.length();
myNewString+=当前[0];

对于(inti=1;i您可能希望删除额外的空白。此代码将有所帮助

string removeWhiteSpace(string current)
{
  string myNewString = "";
  string temp = "";
  int l=current.length();
  myNewString+=current[0];
  for(int i=1;i<l;i++){
      if(myNewString[myNewString.length()-1]==' ' && current[i]==' ')
          continue;
      else
          myNewString+=current[i];
  }
  return myNewString;
}
string removeWhiteSpace(当前字符串)
{
字符串myNewString=“”;
字符串temp=“”;
int l=当前的.length();
myNewString+=当前[0];

对于(inti=1;i而言,问题在于条件

if (temp.back() == ' ' && c != ' ')
如果最后一个字符不是空格且
c
是空格,则需要添加空格:

if (temp.back() != ' ' && c == ' ')
(您反转了
=
!=
运算符)

此外,您还需要在该条件块之后推送新字符
c
(否则
temp.back()
c
将始终是相同的字符)

最后,在开头,字符串
temp
为空,不允许调用
back()
,您应该使用非空字符串来初始化它(例如
temp=“x”

因此,最终有效的功能是:

string removeWhiteSpace(string current)
{
  string myNewString = "";
  string temp = "x";
  for (char c : current)
  {
    if (temp.back() != ' ' && c == ' ')
    {
      myNewString.push_back(' ');
    }
    temp.push_back(c);
    if (c != ' ')
    {
      myNewString.push_back(c);
    }
  }
  return myNewString;
}

问题出在条件反射中

if (temp.back() == ' ' && c != ' ')
如果最后一个字符不是空格且
c
是空格,则需要添加空格:

if (temp.back() != ' ' && c == ' ')
(您反转了
=
!=
运算符)

此外,您还需要在该条件块之后推送新字符
c
(否则
temp.back()
c
将始终是相同的字符)

最后,在开头,字符串
temp
为空,不允许调用
back()
,您应该使用非空字符串来初始化它(例如
temp=“x”

因此,最终有效的功能是:

string removeWhiteSpace(string current)
{
  string myNewString = "";
  string temp = "x";
  for (char c : current)
  {
    if (temp.back() != ' ' && c == ' ')
    {
      myNewString.push_back(' ');
    }
    temp.push_back(c);
    if (c != ' ')
    {
      myNewString.push_back(c);
    }
  }
  return myNewString;
}

更具C++风格的解决方案:

#include <algorithm>
#include <cctype>
#include <string>
#include <utility>
#include <iostream>

std::string remove_excessive_ws(std::string str)
{
    bool seen_space = false;
    auto end{ std::remove_if(str.begin(), str.end(),
                             [&seen_space](unsigned ch) {
                                 bool is_space = std::isspace(ch);
                                 std::swap(seen_space, is_space);
                                 return seen_space && is_space;
                             }
              )
    };

    if (end != str.begin() && std::isspace(static_cast<unsigned>(end[-1])))
        --end;

    str.erase(end, str.end());
    return str;
}

int main()
{
    char const *foo{ "Hello              World!       " };
    std::cout << '\"' << remove_excessive_ws(foo) << "\"\n";
}

更具C++风格的解决方案:

#include <algorithm>
#include <cctype>
#include <string>
#include <utility>
#include <iostream>

std::string remove_excessive_ws(std::string str)
{
    bool seen_space = false;
    auto end{ std::remove_if(str.begin(), str.end(),
                             [&seen_space](unsigned ch) {
                                 bool is_space = std::isspace(ch);
                                 std::swap(seen_space, is_space);
                                 return seen_space && is_space;
                             }
              )
    };

    if (end != str.begin() && std::isspace(static_cast<unsigned>(end[-1])))
        --end;

    str.erase(end, str.end());
    return str;
}

int main()
{
    char const *foo{ "Hello              World!       " };
    std::cout << '\"' << remove_excessive_ws(foo) << "\"\n";
}

首先,这就是你通常解决这类问题的方法。至于其他解决方案,你可以使用字符串流和流迭代器来获取“单词”附加到一个新字符串上,中间只有一个空格。一个可能有用的技巧是找到第一个空格的索引位置并保持该位置的临时值,然后删除该位置和下一个非空格字符之间的所有空格。删除空格后,使用该临时索引值添加回一个空格该位置的空白。这可能不是最有效的方法,但它会解决您的问题。此外,如果有多个空白部分,您可以更新下一组的临时索引位置。首先,请注意,可能重复,这是您通常解决此类问题的方式。至于其他解决方案,您可以uld使用字符串流和流迭代器获取“单词”附加到一个新字符串上,中间只有一个空格。一个可能有用的技巧是找到第一个空格的索引位置并保持该位置的临时值,然后删除该位置和下一个非空格字符之间的所有空格。删除空格后,使用该临时索引值添加回一个空格该位置的空白。这可能不是最有效的方法,但可以解决您的问题。此外,如果有多个空白部分,您可以更新下一组的临时索引位置。@ponikoli:还有一些其他错误,请参阅更新的answer@ponikoli:还有一些其他错误,请参阅更新的回答