How to remove spaces from a string in C++

2 Answers

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

int main()
{
	std::string s = "c c++ c# java python";

	s.erase(std::remove_if(s.begin(), s.end(), std::isspace), s.end());

	std::cout << s << std::endl;

	return 0;
}


/*
run:

cc++c#javapython

*/

 



answered Jun 16, 2017 by avibootz
0 votes
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	std::string s = "c c++ c# java python";

	std::string::iterator end_pos = std::remove(s.begin(), s.end(), ' ');
	s.erase(end_pos, s.end());
	
	std::cout << s << std::endl;

	return 0;
}


/*
run:

cc++c#javapython

*/

 



answered Jun 16, 2017 by avibootz

Related questions

2 answers 155 views
155 views asked May 8, 2021 by avibootz
2 answers 283 views
2 answers 164 views
164 views asked Apr 12, 2020 by avibootz
1 answer 158 views
1 answer 241 views
1 answer 238 views
...