How to remove vowels from a string in C++

4 Answers

0 votes
#include <iostream>
#include <regex>

std::string remove_vowels(std::string s) { 
    std::stringstream result;
    std::regex vowels("[aeiouAEIOU]");
     
    regex_replace(std::ostream_iterator<char>(result), s.begin(), s.end(), vowels, "");
     
    return result.str();
}  
   
int main() 
{ 
    std::string str = "C++ PHP Java Python VB.NET Rust";  
 
    std::cout << remove_vowels(str); 
} 
 
 
 
 
 
/*
run:
 
C++ PHP Jv Pythn VB.NT Rst
 
*/

 



answered Aug 18, 2019 by avibootz
edited Nov 21, 2022 by avibootz
0 votes
#include <iostream>
#include <regex>

std::string remove_vowels(std::string s) { 
    std::regex vowels("[aeiouAEIOU]");

    return regex_replace(s, vowels, "");
}  
   
int main() 
{ 
    std::string str = "C++ PHP Java Python VB.NET Rust";  
 
    std::cout << remove_vowels(str); 
} 
 
 
 
 
 
/*
run:
 
C++ PHP Jv Pythn VB.NT Rst
 
*/

 



answered Nov 21, 2022 by avibootz
0 votes
#include <iostream>
 
int main()
{
    char str[] = "C++ PHP Java Python VB.NET Rust";
  
    int j = 0;
    for (int i = 0; i < str[i]; i++) {
        if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u' &&
            str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') {
            str[j] = str[i];
            j++;
        }
    }
  
    str[j] = '\0';
  
    std::cout << str;
}
  
  
  
  
/*
run:
  
C++ PHP Jv Pythn VB.NT Rst
  
*/

 



answered Dec 26, 2023 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <algorithm>
 
std::string RemoveVowels(std::string str) {
    std::vector<char> vowels = {'a', 'e', 'i', 'o', 'u',
                                'A', 'E', 'I', 'O', 'U'};
      
    for (int i = 0; i < str.length(); i++) {
        if (find(vowels.begin(), vowels.end(), str[i]) != vowels.end()) {
            str = str.replace(i, 1, "");
            i -= 1;
        }
    }
     
    return str;
}
 
 
int main()
{
    std::string str = "C++ PHP Java Python VB.NET Rust";
     
    str = RemoveVowels(str);
     
    std::cout << str;
}
  
  
  
  
  
/*
run:
  
C++ PHP Jv Pythn VB.NT Rst
  
*/
 

 



answered Dec 26, 2023 by avibootz

Related questions

1 answer 124 views
124 views asked Mar 27, 2022 by avibootz
1 answer 115 views
1 answer 130 views
130 views asked Nov 21, 2022 by avibootz
1 answer 127 views
127 views asked Nov 21, 2022 by avibootz
1 answer 133 views
1 answer 139 views
1 answer 113 views
...