Remove Vowels From String Java Program

Let us learn how to remove vowels from string java program.

Well, you will be learning different methods in removing vowel from a string. Below are few methods with example,

Remove Vowels From String Java Program

Remove Vowels From String Java Program

Also Read – Java Example To Print All Alphabets

Method 1: Using replaceAll() function.

import java.util.Scanner;

public class Remove_Vowels_In_String {

public static void main(String[] args) {
 
String actual;
String present;
 
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string: ");
actual = sc.nextLine();
System.out.println("String before removing vowels: " + actual);
present = actual.replaceAll("[aeiouAEIOU]", "");
System.out.println("String after removing vowels: " + present);
}
}

Output:

Please enter a string: Deekshit Prasad
String before removing vowels: Deekshit Prasad
String after removing vowels: Dksht Prsd


Method 2: here in this method we will be calling delete_Vowel() function from the main method. Let’s see the code implementation,

import java.util.Scanner;

public class Remove_Vowels_In_String {

public static void main(String[] args) {

String actual;
String present;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string: ");
actual = sc.nextLine();
System.out.println("String before removing vowels: " + actual);
present = delete_Vowel(actual);
System.out.println("String after removing vowels: " + present);

}

private static String delete_Vowel(String x){

String str_Modified = "";
int m;

for(m = 0;m < x.length();m++)
{
if(!check_Vowel(Character.toLowerCase(x.charAt(m))))
{
str_Modified = str_Modified + x.charAt(m);
}
}
return str_Modified;
}

private static boolean check_Vowel(char y){

String vowels = "aeiou";
for(int n = 0;n < 5;n++)
{
if(y == vowels.charAt(n))
{
return true;
}
}
return false;
}
}

Output:

Please enter a string: Deekshit Prasad
String before removing vowels: Deekshit Prasad
String after removing vowels: Dksht Prsd


Method 3: last but not the least, in this method we are going to use for loop to remove vowels in the given(entered) string. Let’s get into it,

Also Read – Java Program To Find GCD And LCM Of Two Numbers

import java.util.Scanner;

public class Remove_Vowels_In_String {

public static void main(String[] args) {
 
System.out.print("Please enter a string: ");
Scanner sc = new Scanner(System.in);
String actual = sc.nextLine();
System.out.println("String before removing vowels: " + actual);
char word[] = actual.toCharArray();
char modified_Word[] = new char[80];
int y = 0;
 
for(int x = 0;x < word.length;x++)
{
if(word[x] == 'a' || word[x] == 'e' || word[x] == 'i' || word[x] == 'o' || word[x] == 'u')
{
continue;
}
else
{
modified_Word[y] = word[x];
y++;
}
}
System.out.print("String after removing vowels: ");
System.out.print(modified_Word);
}
}

Output:

Please enter a string: Deekshit Prasad
String before removing vowels: Deekshit Prasad
String after removing vowels: Dksht Prsd

About the author

admin

View all posts

Leave a Reply

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