Remove vowels from string java
Written by
Java Program to delete all vowels from a string
Here, we will create a program that asks for String input from the user and displays the string after eliminating or rather removing all the vowel characters.
The basic idea is to calculate the length, say N, of the input string and then run a Loop-construct that will iterate from 0 to (N-1). At every iteration, it will extract the Ith character from the string and compare if it is a vowel character, either in lower case or in upper case, if yes then a simple counter is increased (Doesn’t have any utility). If the comparison is found to be false (i.e. not a vowel character), accumulate the extracted character into a final String Variable. Display the result.
Java Code:
/* Program to input a string and print it after removing all vowels*/
import java.util.*;
class RemoveVowels
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter String: ");
String s=inp.nextLine();
int k=s.length();
int i,f=1;
String z="";
char c;
for(i=0;i<k;i++)
{
c=s.charAt(i);
if(c=='A' || c=='a' || c=='E' || c=='e' || c=='I' || c=='i' || c=='O' || c=='o' ||c=='U' || c=='u')
f=0;
else
z=z+c;
}
System.out.println("String Without Vowels: "+z);
}
}
Output:
Enter String: Hauntings of The Hill House
String Without Vowels: Hntngs f Th Hll Hs
Enter String: My Name is XYZ
String Without Vowels: My Nm s XYZ