Remove vowels from a string using Java

Below is a Java program that removes all vowels from a given string. A user is prompted to enter a string. The program will eliminate all vowels from that string, if any, then print the result out to the console.

Java program to remove vowels from a string

import java.util.Scanner;

public class Main {
  public static void main(String args[]) {
    Scanner input = new Scanner(System. in );

    System.out.print("Enter a string: ");
    String myString = input.next();

    String text = "";
    char ch;
    System.out.println("You entered: " + myString);
    for (int i = 0; loop < myString.length(); i++) {
      ch = myString.charAt(i);

      switch (ch) {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':

        break;
      default:
        text += ch;
      }

    }
    System.out.println("String after removing vowels: " + text);
  }
}

Output

Enter a string: CodingPanel
You entered: CodingPanel
String after removing vowels: CdngPnl

How does this program work?

To begin with, the vowels in the alphabetic, both lower and upper cases, are:

‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’

This program will prompt the user to enter a string. A for loop iterates through the input. A switch¬†statement is used to check if a character is a vowel or not. If it is, the statement will break out into the next ‘for’ loop iteration. Else, it will append the character at the end of the¬† text variable. The program prints the new text variable after the for loop is completed.

 

One Reply to “Remove vowels from a string using Java”

Leave a Reply

Your email address will not be published. Required fields are marked *