How to remove all characters from a string except the alphabets in C++

1 Answer

0 votes
#include <iostream>

using namespace std;

string remove_characters_except_alphabets(string s) { 
    for (int i = 0; i < s.size(); i++) { 
        if (s[i] < 'A' || s[i] > 'Z' && s[i] < 'a' || s[i] > 'z') {    
            s.erase(i, 1);  
            i--; 
        } 
    } 
    return s; 
} 

int main() 
{ 
    string s = "c++, programming.. version$! 14"; 
    
    s = remove_characters_except_alphabets(s);  
    
    cout << s;
    
    return 0; 
} 


/*
run:

cprogrammingversion

*/

 



answered Feb 1, 2019 by avibootz
...