How to remove spaces from string in C++

2 Answers

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

int main() {
    std::string s = "CPP is a, general purpose programming language.";

    s.erase(std::remove(s.begin(), s.end(), ' '), s.end());

    std::cout << s;
    
    return 0;
}




/*
run:

CPPisa,generalpurposeprogramminglanguage.

*/

 



answered May 8, 2021 by avibootz
0 votes
#include <iostream>
#include <algorithm>

int main() {
    std::string s = "CPP is a, general purpose programming language.";

    s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end());
    
    std::cout << s;
    
    return 0;
}




/*
run:

CPPisa,generalpurposeprogramminglanguage.

*/

 



answered May 8, 2021 by avibootz

Related questions

2 answers 283 views
2 answers 163 views
163 views asked Apr 12, 2020 by avibootz
1 answer 158 views
1 answer 241 views
2 answers 159 views
159 views asked Jun 16, 2017 by avibootz
1 answer 238 views
...