Program to remove consonants from a String - GeeksforGeeks

09 Jun, 2021
Given a string , the task is to remove all the consonants from the string and then print the string.
Examples:  
Input: str= “Welcome to geeksforgeeks” Output: eoe o eeoee
Input: str= “What is your name?” Output: a i ou ae? 
Approach: Traverse all the characters of the string, if the character is a consonant then remove it from the final answer.
Below is the implementation of the above approach:
Java
Python3
C#
Javascript
// Java program to remove consonants from a Stringimport java.util.Arrays;import java.util.List; class Test {     // function that returns true    // if the character is an alphabet    static boolean isAlphabet(char ch)    {        if (ch >= 'a' && ch <= 'z')            return true;        if (ch >= 'A' && ch <= 'Z')            return true;        return false;    }     // function to return the string after    // removing all the consonants from it    static String remConsonants(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 (isAlphabet(sb.charAt(i))                && !al.contains(sb.charAt(i))) {                sb.replace(i, i + 1, "");                i--;            }        }         return sb.toString();    }     // Driver method to test the above function    public static void main(String[] args)    {        String str            = "GeeeksforGeeks - A Computer Science Portal for Geeks";         System.out.println(remConsonants(str));    }}
# Python3 program to remove consonants from a String  # function that returns true# if the character is an alphabetdef isAlphabet(ch):     if (ch >= 'a' and ch <= 'z'):        return True;    if (ch >= 'A' and ch <= 'Z'):        return True;    return False; # Function to return the string after# removing all the consonants from itdef remConsonants(str):    vowels = [ 'a', 'e', 'i', 'o', 'u',  'A', 'E', 'I', 'O', 'U' ]        sb = "";       for i in range(len(str)):           present = False;               for j in range(len(vowels)):               if (str[i] == vowels[j]):                           present = True;                break;                if (not isAlphabet(str[i]) or present ):               sb += str[i];    return sb; # Driver codeif __name__=='__main__':       str = "GeeeksforGeeks - A Computer Science Portal for Geeks";    print(remConsonants(str));     # This code is contributed by pratham76
// C# program to remove consonants from a Stringusing System;using System.Collections;using System.Collections.Generic;using System.Linq; class GFG{  // Function that returns true// if the character is an alphabetstatic bool isAlphabet(char ch){    if (ch >= 'a' && ch <= 'z')        return true;    if (ch >= 'A' && ch <= 'Z')        return true;             return false;} // Function to return the string after// removing all the consonants from itstatic string remConsonants(string str){    char []vowels = { 'a', 'e', 'i', 'o', 'u',                      'A', 'E', 'I', 'O', 'U' };         string sb = "";     for(int i = 0; i < str.Length; i++)    {        bool present = false;        for(int j = 0; j < vowels.Length; j++)        {            if (str[i] == vowels[j])            {                present = true;                break;            }        }                 if (!isAlphabet(str[i]) || present )        {            sb += str[i];        }    }    return sb;} // Driver codepublic static void Main(string[] args){    string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";     Console.Write(remConsonants(str));}} // This code is contributed by rutvik_56
<script>       // JavaScript program to remove      // consonants from a String             // Function that returns true      // if the character is an alphabet      function isAlphabet(ch) {        if (ch >= "a" && ch <= "z") return true;        if (ch >= "A" && ch <= "Z") return true;         return false;      }       // Function to return the string after      // removing all the consonants from it      function remConsonants(str) {        var vowels =        ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];         var sb = "";         for (var i = 0; i < str.length; i++) {          var present = false;          for (var j = 0; j < vowels.length; j++) {            if (str[i] === vowels[j]) {              present = true;              break;            }          }           if (!isAlphabet(str[i]) || present) {            sb += str[i];          }        }        return sb;      }       // Driver code      var str =      "GeeeksforGeeks - A Computer Science Portal for Geeks";       document.write(remConsonants(str));           </script>
eeeoee - A oue iee oa o ee
 
Below is another program using Regular Expressions in Java 
Java
C#
// Java program to remove consonants from a String class Test{       static String remVowel(String str)    {         return str.replaceAll("[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");    }         // Driver method to test the above function    public static void main(String[] args)    {        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";                 System.out.println(remVowel(str));    }}
// C# program to remove consonants from a Stringusing System;using System.Text.RegularExpressions; class GFG{    static String remVowel(String str)    {        return Regex.Replace(str, "[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");    }         // Driver code    public static void Main()    {        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";                 Console.WriteLine(remVowel(str));    }} // This code is contributed by Rajput-Ji
eeeoee - A oue iee oa o ee
 
Time complexity : O(n) where n is the length of string 
VishalBachchas
Rajput-Ji
rutvik_56
pratham76
rdtank
java-regular-expression
Technical Scripter 2018
vowel-consonant
Java Programs
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
Java Program to Read a File to String
How to Write Data into Excel Sheet using Java?
Java Servlet and JDBC Example | Insert data in MySQL
Removing last element from ArrayList in Java
How to Replace a Element in Java ArrayList?
Java Program to Find Sum of Array Elements
Tic-Tac-Toe Game in Java