Remove vowels from a string in java

Hey guys!! Welcome to flower brackets blog. In this article we are going to learn remove vowels from a string in java.

Remove vowels from a string in java

Here’s the java program to remove all vowels from string using StringBuffer class,

Also read – remove vowels from string using switch case

import java.util.Scanner;

public class RemoveVowelJava
{
public static String removeVowel(String str)
{
StringBuffer sb = new StringBuffer();
for(int a = 0; a < str.length(); a++)
if((a % 2) == 0)
sb.append(str.charAt(a));
else if((a % 2) != 0)
if(str.charAt(a) != 'a' && str.charAt(a) != 'e' && str.charAt(a) != 'i'
&& str.charAt(a) != 'o' && str.charAt(a) != 'u')
if(str.charAt(a) != 'A' && str.charAt(a) != 'E' && str.charAt(a) != 'I'
&& str.charAt(a) != 'O' && str.charAt(a) != 'U')
sb.append(str.charAt(a));
return sb.toString();
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a sentence to remove vowels : ");
String strInput = sc.nextLine();
System.out.println("After remove vowels from string : " + removeVowel(strInput));
sc.close();
}
}

Output:

Please enter a sentence to remove vowels : Welcome to Flower Brackets blog!!
After remove vowels from string : Wlcome t Flwr Brackts blg!!

Related Posts