Java Program to Remove All Vowels from a String

Java Program to Remove All Vowels from a String is one of the commonly asked interview questions related to Java Programming.

steps:

  1. Initialize the String str = “Test Automation Central”;
  2. For the vowels, we will initialize String vowels = “aeiouAEIOU”; and initialize an empty string for the output.
  3. Write enhanced for loop – for(char c :str.toCharArray())
  4. To check whether the provided String character is a vowel or not, write if(vowels.indexOf(c) == -1) condition.
  5. 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
Share

Leave a Reply