Program to remove consonants from a String
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 eeoeeInput: 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 program to remove consonants from a String import 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)); } } |
eeeoee - A oue iee oa o ee
Below is another program using Regular Expressions in Java
Java
// 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#
// C# program to remove consonants from a String using 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
Recommended Posts:
- Replace consonants with next immediate consonants alphabetically in a String
- Program to remove vowels from a String
- Modify string by removing vowels in between two consonants
- Replace all consonants with nearest vowels in a string
- Count consonants in a string (Iterative and recursive methods)
- Modify the string by swapping continuous vowels or consonants
- Encrypt string with product of number of vowels and consonants in substring of size k
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Remove characters from a numeric string such that string becomes divisible by 8
- Remove new lines from string in PHP
- How to remove the first character of string in PHP?
- Remove a given word from a String
- Remove consecutive vowels from string
- How to remove all white spaces from a String in Java?
- Remove characters from string that appears strictly less than K times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



