Program to remove vowels from a String
Last Updated :
16 Feb, 2023
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.
Implementation:
C++14
// C++ program to remove vowels from a String
#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;
}
// Driver Code
int main()
{
string str = "GeeeksforGeeks - A Computer"
" Science Portal for Geeks";
cout << remVowel(str) << endl;
return 0;
}
// This code is contributed by
// sanjeev2552
// and corrected by alreadytaken
Java
import java.util.Scanner;
public class Practice {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e'
|| s.charAt(i) == 'i' || s.charAt(i) == 'o'
|| s.charAt(i) == 'u' || s.charAt(i) == 'A'
|| s.charAt(i) == 'E' || s.charAt(i) == 'I'
|| s.charAt(i) == 'O'
|| s.charAt(i) == 'U') {
continue;
}
else {
System.out.print(s.charAt(i));
}
}
}
}
Python3
# Python program to remove vowels from a string
# Function to remove vowels
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)
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
rem_vowel(string)
string = "Loving Python LOL"
rem_vowel(string)
C#
// C# program to remove vowels from a String
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();
}
// Driver method to test the above function
public static void Main()
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
Console.Write(remVowel(str));
}
}
//This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to remove
// vowels from a String
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;
}
// Driver code
let str = "GeeeksforGeeks - A Computer Science " +
"Portal for Geeks";
document.write(remVowel(str));
// This code is contributed by rag2127
</script>
OutputGksfrGks - Cmptr Scnc Prtl fr Gks
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
We can improve the above solution by using Regular Expressions.
Implementation:
C++
// C++ program to remove vowels from a String
#include <bits/stdc++.h>
using namespace std;
string remVowel(string str)
{
regex r("[aeiouAEIOU]");
return regex_replace(str, r, "");
}
// Driver Code
int main()
{
string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
cout << (remVowel(str));
return 0;
}
// This code is contributed by Arnab Kundu
Java
// Java program to remove vowels from a String
import java.util.Arrays;
import java.util.List;
class GFG
{
static String remVowel(String str)
{
return str.replaceAll("[aeiouAEIOU]", "");
}
// Driver Code
public static void main(String[] args)
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
System.out.println(remVowel(str));
}
}
Python3
# Python program to remove vowels from a string
# Function to remove vowels
# import the module for regular expression (re)
import re
def rem_vowel(string):
return (re.sub("[aeiouAEIOU]","",string))
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
print(rem_vowel(string))
C#
// C# program to remove vowels from a String
using System;
using System.Text.RegularExpressions;
class GFG
{
static String remVowel(String str)
{
str = Regex.Replace(str, "[aeiouAEIOU]", "");
return str;
}
// Driver code
public static void Main()
{
String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
Console.WriteLine(remVowel(str));
}
}
//This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to remove vowels from a String
function remVowel(str)
{
let r= new RegExp("[aeiouAEIOU]",'g')
return str.replace(r,"");
}
// Driver Code
let str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
document.write(remVowel(str));
// This code is contributed by Pushpesh Raj.
</script>
OutputGksfrGks - Cmptr Scnc Prtl fr Gks
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
Similar Reads
Program to remove vowels from Linked List Given a singly linked list, the task is to remove the vowels from the given linked list. Examples: Input: g -> e -> e -> k -> s -> f -> o -> r -> g -> e -> e -> k -> s Output: g -> k -> s -> f -> r -> g -> k -> s Explanation: After removing vo
15 min read
Program to duplicate Vowels in String Given a string "str", the task is to duplicate vowels in this string. Examples: Input: str = "geeks"Output: geeeeks Input: str = "java"Output: jaavaa Approach: Iterate the string using a loop. Check if the character is a vowel and duplicate it. Return then print the resultant string. Below is the im
5 min read
Remove consecutive vowels from string Given a string s of lowercase letters, we need to remove consecutive vowels from the string Note : Sentence should not contain two consecutive vowels ( a, e, i, o, u). Examples : Input: geeks for geeksOutput: geks for geksInput : your article is in queue Output : yor article is in quApproach: Iterat
15+ min read
Print reverse string after removing vowels Given a string s, print reverse of string and remove the characters from the reversed string where there are vowels in the original string. Examples: Input : geeksforgeeksOutput : segrfsegExplanation :Reversed string is skeegrofskeeg, removing characters from indexes 1, 2, 6, 9 & 10 (0 based ind
13 min read
Program to accept Strings starting with a Vowel Given string str consisting of alphabets, the task is to check whether the given string is starting with a Vowel or Not. Examples: Input: str = "Animal" Output: Accepted Input: str = "GeeksforGeeks" Output: Not Accepted Approach: Find the first character of the stringCheck if the first character of
4 min read