replacing double space to single space

后端 未结 1 1108
夕颜
夕颜 2021-01-27 02:28

How can I replace the double space into single space using C++

ex:

\"1  2  3  4  5\" => \"1 2 3 4 5\"

this is what I`ve done till no

相关标签:
1条回答
  • 2021-01-27 02:38

    In while loop of splitLines code, use this code.

      while((loc = line.find("  ")) != std::string::npos) //Two spaces here
      {
           line.replace(loc,2," "); //Single space in quotes
      }
      cout << line << endl;
    

    Thats it. I haven't tried it out, let me know if it works.

    And as fred pointed out, use pass by reference in splitLines function. The above solution is sub-normal and is O(n^2) complexity. This one is better.

      int loc = -1;
      while((loc = line.find("  ",loc+1)) != std::string::npos) //Two spaces here
      {
           line.replace(loc,2," "); //Single space in quotes
      }
      cout << line << endl;
    
    0 讨论(0)
提交回复
热议问题