Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Java Program to remove vowels from an input string
August 20, 2021
Remove Vowels from a String
In this article we will learn how to code a Java program to remove vowels from a string. ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters. Java programming is case sensitive, and hence lowercase and uppercase characters are considered differently, so we will have to check for both the cases and then we can remove the vowels from a string
Approach
Step 1: Take input str as a String.
Step 2: Convert the string into character array.
Step 3: Traverse the complete array and check if the character present at particular index is vowel or not.
Step 4: If the character if vowel the continue the loop else concatenate the character into resultant string.
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String ss=””;
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!='A' && c!='E' && c!='I' && c!='O' && c!='U')
ss=ss+c;
}
System.out.print(ss);
}
}
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int l=s.length();
char c;
String ss=””;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!='A' && c!='E' && c!='I' && c!='O' && c!='U')
{
ss=ss+c;
}
}
System.out.print(ss);
}
}