Java Program to Remove All Vowels from a String is one of the commonly asked interview questions related to Java Programming.
steps:
- Initialize the String str = “Test Automation Central”;
- For the vowels, we will initialize String vowels = “aeiouAEIOU”; and initialize an empty string for the output.
- Write enhanced for loop – for(char c :str.toCharArray())
- To check whether the provided String character is a vowel or not, write if(vowels.indexOf(c) == -1) condition.
- Finally, we will print the output where all the vowels are removed from the String.
Java Program to Remove All Vowels from a String
package TestAutomationCentral;
public class RemoveVowels {
public static void main(String[] args) {
String str = "Test Automation Central";
String vowels = "aeiouAEIOU";
String output = "";
for(char c :str.toCharArray()){
if(vowels.indexOf(c) == -1){
output = output + c;
}
}
System.out.println(output);
}
}
OUtput:
Tst tmtn Cntrl