How to move all special characters to the end of a string in C++

1 Answer

0 votes
#include <iostream>

using namespace std;

string move_special_characters_to_end(string s) { 
    int len = s.length(); 
  
    string chars = "", pecial_characters = ""; 
    for (int i = 0; i < len; i++) { 
        char ch = s.at(i); 
        if (isalnum(ch) || ch == ' ') 
            chars += ch; 
        else
            pecial_characters += ch; 
    } 
    return chars + pecial_characters; 
} 
  
int main() 
{ 
    string s("c++£$vb.net&%java*() php <>/python 3.7.3"); 

    cout << move_special_characters_to_end(s);
    
    return 0; 
} 


/*
run:

cvbnetjava php python 373++£$.&%*()<>/..

*/

 



answered Aug 11, 2019 by avibootz

Related questions

1 answer 181 views
1 answer 275 views
2 answers 353 views
2 answers 345 views
1 answer 268 views
2 answers 303 views
...