How to remove all double spaces from a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
	string s = "c,  c++,  java,  python";

	std::size_t doubleSpace = s.find("  ");
	while (doubleSpace != string::npos)
	{
		s.erase(doubleSpace, 1);
		doubleSpace = s.find("  ");
	}

	cout << s << endl;

	return 0;
}


/*
run:

c, c++, java, python

*/

 



answered Jan 27, 2018 by avibootz

Related questions

2 answers 163 views
163 views asked Apr 12, 2020 by avibootz
1 answer 207 views
2 answers 154 views
154 views asked May 8, 2021 by avibootz
2 answers 283 views
1 answer 241 views
2 answers 159 views
159 views asked Jun 16, 2017 by avibootz
...