Remove Vowels from a given String

Objective: Given a String, remove all the vowels from the string.

What Are Vowels?
The letters A, E, I, O, and U are called vowels. The other letters in the alphabet are called consonants.

Example:

Input String: algorithms @ tutorial horizon
Updated String: lgrthms @ ttrl hrzn

Approach:

Iterate through all the characters, if find vowel, skip it else add it to the result.

Code:

public class RemoveVowels {
public void removeVowels(String input){
StringBuffer result = new StringBuffer();
char[] chars = input.toCharArray();
for (int i = 0; i <chars.length ; i++) {
char chr = chars[i];
if(!isVowel(chr))
result.append(chr);
}
System.out.println("Updated String: " + result);
}
public boolean isVowel(char chr){
String vowels="AEIOUaeiou";
if(vowels.contains(chr+""))
return true;
else
return false;
}
public static void main(String[] args) {
RemoveVowels r = new RemoveVowels();
String input = "algorithms @ tutorial horizon";
System.out.println("Input String: " + input);
r.removeVowels(input);
}
}

view raw
RemoveVowels.java
hosted with ❤ by GitHub


Output:

Input String: algorithms @ tutorial horizon
Updated String: lgrthms @ ttrl hrzn

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.