How to remove all spaces from string in C++

2 Answers

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

int main()
{
    std::string s = "Remove all spaces from string in C++";
    
    s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
    
    std::cout << s << std::endl;
 
    return 0;
    
}


/*
run:

RemoveallspacesfromstringinC++

*/

 



answered Apr 12, 2020 by avibootz
0 votes
#include <iostream>
#include <algorithm>

int main()
{
    std::string s = "Remove all spaces from string in C++";
    
    s.erase(std::remove_if(s.begin(), s.end(), [](unsigned char ch){
        return std::isspace(ch);
    }), s.end());
    
    std::cout << s << std::endl;
 
    return 0;
    
}


/*
run:

RemoveallspacesfromstringinC++

*/

 



answered Apr 12, 2020 by avibootz

Related questions

1 answer 158 views
2 answers 155 views
155 views asked May 8, 2021 by avibootz
2 answers 283 views
1 answer 241 views
2 answers 160 views
160 views asked Jun 16, 2017 by avibootz
1 answer 238 views
...