Given a string, remove the vowels from the string and print the string without vowels.
Examples:
Input : welcome to geeksforgeeks
Output : wlcm t gksfrgks
Input : what is your name ?
Output : wht s yr nm ?
A loop is designed that goes through a list composed of the characters of that string, removes the vowels and then joins them.
C++14
#include <bits/stdc++.h>
using namespace std;
string remVowel(string str)
{
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()
{
string str = "GeeeksforGeeks - A Computer"
" Science Portal for Geeks" ;
cout << remVowel(str) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
class Test
{
static String remVowel(String str)
{
Character vowels[] = { 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' };
List<Character> al = Arrays.asList(vowels);
StringBuffer sb = new StringBuffer(str);
for ( int i = 0 ; i < sb.length(); i++) {
if (al.contains(sb.charAt(i))){
sb.replace(i, i+ 1 , "" ) ;
i--;
}
}
return sb.toString();
}
public static void main(String[] args)
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks" ;
System.out.println(remVowel(str));
}
}
|
Python3
def rem_vowel(string):
vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' ]
result = [letter for letter in string if letter.lower() not in vowels]
result = ''.join(result)
print (result)
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
rem_vowel(string)
string = "Loving Python LOL"
rem_vowel(string)
|
C#
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
public class Test
{
static String remVowel(String str)
{
char []vowels = { 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' };
List< char > al = vowels.OfType< char >().ToList();;
StringBuilder sb = new StringBuilder(str);
for ( int i = 0; i < sb.Length; i++) {
if (al.Contains(sb[i])){
sb.Replace(sb[i].ToString(), "" ) ;
i--;
}
}
return sb.ToString();
}
public static void Main()
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks" ;
Console.Write(remVowel(str));
}
}
|
Javascript
<script>
function remVowel(str)
{
let al = [ 'a' , 'e' , 'i' , 'o' , 'u' ,
'A' , 'E' , 'I' , 'O' , 'U' ];
let result = "" ;
for (let i = 0; i < str.length; i++)
{
if (!al.includes(str[i]))
{
result += str[i];
}
}
return result;
}
let str = "GeeeksforGeeks - A Computer Science " +
"Portal for Geeks" ;
document.write(remVowel(str));
</script>
|
OutputGksfrGks - Cmptr Scnc Prtl fr Gks
We can improve the above solution by using Regular Expressions.
C++
#include <bits/stdc++.h>
using namespace std;
string remVowel(string str)
{
regex r( "[aeiouAEIOU]" );
return regex_replace(str, r, "" );
}
int main()
{
string str = "GeeeksforGeeks - A Computer Science Portal for Geeks" ;
cout << (remVowel(str));
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
class GFG
{
static String remVowel(String str)
{
return str.replaceAll( "[aeiouAEIOU]" , "" );
}
public static void main(String[] args)
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks" ;
System.out.println(remVowel(str));
}
}
|
Python3
import re
def rem_vowel(string):
return (re.sub( "[aeiouAEIOU]" ,"",string))
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
print (rem_vowel(string))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static String remVowel(String str)
{
str = Regex.Replace(str, "[aeiouAEIOU]" , "" );
return str;
}
public static void Main()
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks" ;
Console.WriteLine(remVowel(str));
}
}
|
OutputGksfrGks - Cmptr Scnc Prtl fr Gks
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.