Want to delete vowels from a given string in c++? Use the following c++ example to delete vowels from string using c++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> #include <cstring> using namespace std; int vowel(char c) { if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c =='o' || c=='O' || c == 'u' || c == 'U') return 1; // a vowel else return 0; // not a vowel } int main() { string str,newstr; cout<<"Enter a string: "; getline(cin,str); int len=str.length(); int j=0; for(int i = 0; i<len; i++) { if(vowel(str[i]) == 0) { newstr[j] = str[i]; //newstr is string without vowels j++; } } newstr[j] = '\0'; //terminate the string strcpy(str, newstr); //copying the new string, cout<<"Modified String:"<<str; return 0; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.