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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Output:
Input String: algorithms @ tutorial horizon Updated String: lgrthms @ ttrl hrzn