Table of Contents

In Java, to remove all vowels from a given string, we can use loop concepts such as a while loop or a for loop.

For example, if the input string is “Vowel”, then the output string after removing the vowels from input string is “Vwl”.

In this article we will learn how to remove vowels from a given string through an example, detailed logic and program explanation for better understanding.

Required Knowledge

  • Java programming basics
  • Java operators
  • Looping concepts
  • Conditional statements

Problem statement

We need to write a Java program to remove all vowels from a given string. The program should take an input string and remove all the vowels, leaving only consonants, digits, and special characters. This task will be achieved in our program. Let’s see an example,

Example:

For a given input String str “Problem”, Output should be “Prblm”.

Explanation:

Input String str: "Problem"

Consonants: Prblm

Vowels: oe

If a character is A, E, I, O, U, a, e, i, o, u, we have to remove it from the given string str.

After removing vowels, str is: Prblm

Hence, we will get "Prblm" as the result for the input "Problem".

Logic to remove all vowels from a given string in Java

There are multiple logics to remove all vowels from a given string in the Java programming language.

Logic 1: Using while loop

To remove all vowels using while loop from a given string:

  • Initialize input string str from user.
  • Trim any leading or trailing whitespace from str.
  • Use while loop to iterate over each character in the string (i < str.length()).
  • Inside the loop:
    • Extract the character at the current index (i) using str.charAt(i).
    • Check if the character is not a vowel (A, E, I, O, U, a, e, i, o, u) using an if statement.
  • Print the character if it is not vowel.
  • Increment the index i to move to the next character.

Logic 2: Using for loop

To remove all vowels using a for loop from a given string:

  • Initialize an input string str from user input.
  • Trim any leading or trailing whitespace from str.
  • Initialize an empty string result to store the vowel-removed string.
  • Use a for loop to iterate over each character in the string. Inside the loop:
    • Retrieve the character at the current index i using str.charAt(i).
    • Check if the character ch is a vowel (A, E, I, O, U, a, e, i, o, u) using an if statement.
    • If ch is a vowel, skip to the next iteration using continue.
    • If ch is not a vowel, concatenate it to the result string.
  • After the loop completes, print the result string, which contains the input string with all vowels removed.

Java program to remove all vowels from a given string

Program 1: Using while loop

In the below program we are using while loop to remove all vowels from a given string in Java.

import java.util.Scanner;
public class RemoveVowels {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a text");
		String str = s.nextLine();
		str= str.trim();
		int i = 0;
		while (i < str.length()) {
			char ch = str.charAt(i);
			if (!(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i'
					|| ch == 'o' || ch == 'u')) {
				System.out.print(ch);
			}
			i++;
		}
		s.close();
	}
}

Output:

Enter a text
Hello World
Hll Wrld

Java program explanation to remove all vowels from a given string

  1. The java program imports the Scanner class from java.util package.
  2. Define a public class named RemoveVowels.
  3. Define the main method inside the RemoveVowels class.
  4. Create a Scanner object ‘s’ to read input from the user.
  5. Ask the user to enter text.
  6. Read the input text entered by the user using the nextLine() method of Scanner class and store it in the ‘str’ variable.
  7. Trim the input str to remove any leading or trailing whitespaces.
  8. Initialize an integer variable ‘i’ to 0 to use as an index for iterating through the characters of the str.
  9. Use a while loop to iterate through each character of the str.
  10. For each character:
    • Check if it is not a vowel by comparing it with a list of vowel characters (‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’) using the logical NOT (!) operator.
    • If it is not a vowel, print the character.
  11. Increment the index ‘i’.
  12. Close the Scanner object ‘s’ to release system resources.

Program 2: Using for loop

In the below program we are using for loop to remove all vowels from a given string in Java.

import java.util.Scanner;
public class RemoveVowels2 {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a text");
		String str = s.nextLine();
         str = str.trim();
         String result="";
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i'
					|| ch == 'o' || ch == 'u') {
             continue;
			}  
            	 result += ch;     
			}
			 System.out.println("After removing vowels: " + result);
		s.close();
	}
}

Output:

Enter a text
Expert Learning
After removing vowels: xprt Lrnng

Java program explanation to remove all vowels from a given string

  1. The java program imports the Scanner class from java.util package.
  2. Define a public class named RemoveVowels2.
  3. Define the main method inside the RemoveVowels2 class.
  4. Create a Scanner object ‘s’ to read input from the user.
  5. Ask the user to enter text.
  6. Read the input text entered by the user using the nextLine() method of Scanner class and store it in the ‘str’ variable.
  7. Trim the input str to remove any leading or trailing whitespaces.
  8. Initializes an empty string result to store the vowel-removed text.
  9. Use a for loop to iterate through each character of the str.
  10. For each character:
    • Check if it is a vowel by comparing it with a list of vowel characters (‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’).
    • If it is a vowel, continue to the next iteration of the loop.
    • Appends the character to result if it’s not a vowel.
    • Prints the final string after removing vowels.
  11. Close the Scanner object ‘s’ to release system resources.

Conclusion

In this article, we have learnt how to remove vowels from a given string in Java. We have seen the logic and implemented the program using while loop and for loop in Java. Hope this article helped you in the understanding of the program.

You can also check other amazing tutorials

  1. Java program to print all prime numbers in a range
  2. Swap first and last digit of a number in Java
  3. Print HCF of two numbers in Java
  4. Java program to print sum of digits of a number

Happy Programming!!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Categorized in:

Java Coding,