In java, to remove all consonants from a given string we can use conditional statements such as if-else or looping statements such as for loops.
For example, given input string is “Hi”, then the output should be “i” after removing the consonants from the string.
In this article we will learn to remove all consonants from a string through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java opertors
- Looping concepts
- Conditional statements
Problem statement
We need to write a Java program to remove all consonants from a given string. This program should take an input string and remove all alphabets representing consonants, while preserving vowels, digits, and special characters in string. This will be achieved in our program. Let’s see an example,
Example:
For a given input String str “Hello”, Output should be “eo”.
Explanation:
Input String str: "Hello"
Vowels: e and o
Consonants: H, l and l
We should remove consonants and should print only vowels (in this case)
If characters in the string str is equal to the any one of these vowels A,E,I,O,U and a,e,i,o,u, then we should print the characters.
In str, e and o matches
Hence, the resulting output will be "eo".
Logic to remove all consonants from a given string
There are multiple logics to remove consonants from a given string in the java programming language.
Logic 1: Using if-else
- In order to remove consonants from a string, begin by initializing the string (str), and trim any leading or trailing spaces using trim().
- Use an if-else statement to check if the trimmed text is not empty. If it’s not empty, proceed to the next step.
- Use the replaceAll() method with a regular expression to replace all consonants (both uppercase and lowercase) with an empty string.
- If the input string (str) was empty, assign an empty string to it.
- Print the modified string (str) to display the result.
Logic 2: Using for loop
- In order to remove consonants from a string, begin by initializing the string (str), and trim any leading or trailing spaces using trim().
- Use a loop to go through each character in the str.
- Extract the character at current index i using charAt(i) and assign it to the variable ch.
- Using if else, verify if ch is an alphabet character by checking if it falls within the ASCII range of uppercase (‘A’ to ‘Z’) or lowercase (‘a’ to ‘z’) letters.
- If ch is an alphabet character, further check if it is a vowel.
- If ch is a vowel, print it. Otherwise, if ch is a digit or a special character, print it as well.
Java program to remove all consonants from a given string
Program 1: Using if-else
In the below program we are using if-else to remove all the consonants from a given string.
import java.util.Scanner;
public class RemoveConsonants {
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();
if(!str.isEmpty()) {
str = str.replaceAll("[BCDFGHJKLMNPQRSTVWXYXbcdfghjklmnpqrstvwxyz]", "");
}
else {
str="";
}
System.out.println("Output is: "+ str);
s.close();
}
}
Output:
Enter a text
Interview Expert
Output is: Ieie Ee
Java program explanation to remove all consonants from a given string
- The java program imports the Scanner class from java.util package.
- Define a public class named RemoveConsonants.
- Define the main method inside the RemoveConsonants class.
- Create a Scanner object ‘s’ to read input from the user.
- Ask the user to enter text.
- Read the input text entered by the user using the nextLine() method of Scanner class and store it in the ‘str’ variable.
- Trim the input str to remove any leading or trailing whitespaces.
- Check if the input str is not empty using the isEmpty() method.
- If the input str is not empty, remove all consonants (both uppercase and lowercase) from the str using the replaceAll() method with a regular expression.
- If the input string str is empty, assign an empty string to the ‘str’ variable.
- Print the modified str after removing consonants.
- 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 the consonants from a given string.
import java.util.Scanner;
public class RemoveConsonants2 {
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();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
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);
} else {
System.out.print(ch);
}
}
s.close();
}
}
Output:
Enter a text
Happy Learning
a eai
Java program explanation to remove all consonants from a given string
- The java program imports the Scanner class from java.util package.
- Define a public class named RemoveConsonants2.
- Define the main method inside the RemoveConsonants2 class.
- Create a Scanner object ‘s’ to read input from the user.
- Ask the user to enter text.
- Read the input text entered by the user using the nextLine() method of Scanner class and store it in the ‘str’ variable.
- Trim the input str to remove any leading or trailing whitespaces.
- Iterate through each character in the str using a for loop.
- For each character:
- Check if it is an alphabet letter by comparing its ASCII value with the range of uppercase and lowercase letters (‘A’ to ‘Z’ and ‘a’ to ‘z’).
- If it is an alphabet letter, 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, print the character.
- If it is not a vowel (i.e., it’s a consonant or any other character like digits or symbols), print the character as it is.
- Close the Scanner object ‘s’ to release system resources.
Conclusion
The Java programs we’ve implemented here remove all consonants from a given string using if-else statements and for loops.