Java Program to Remove Vowels from a String
Program to remove vowels from String
In this problem, we’re going to code a java Program to remove vowels from String. Take a string input from the user and store it in a variable called as “s” (in this case) .‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters.
Algorithm
- Take a String input from user and store is in a variable called as s
- After that take String variable s1 with empty String
- After that call replaceAll() on s object
- Write regex on replaceAll() method like this s1 = s.replaceAll(“[aeiou]”, “”);
- Print s variable
Note
Here we will use replaceAll() method to replace vowel from string the main work of replaceAll() method in java is to replace something by passing two arguments to this method i.e one is an old string and second is new string that you're replacing the old string with, so here we're passing a regular expression to replace vowels with blank spaces.
Code in Java
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { String s = "prepinsta"; String s1 = ""; s1 = s.replaceAll("[aeiou]", ""); System.out.println("String after removing vowel : "+s1); } }
Output
String after removing vowel : prpnst
Method 2
We have used regex in this code.
Run
// Java program to remove vowels from a String import java.util.Arrays; import java.util.List; class Main { static String remVowel(String str) { return str.replaceAll("[aeiouAEIOU]", ""); } // Driver Code public static void main(String[] args) { String str = "Prepinsta"; System.out.println(remVowel(str)); } }
Output
Prpnst
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
public static void main(String[] args) {
String str=”Shekhar Suman”;
str=str.toLowerCase();
String v=”aeiou”;
for (int i=0;i<v.length();i++){
str=str.replaceAll(String.valueOf(v.charAt(i)),"");
}
System.out.println(str);
}
Hey, join our Discord Channel for technical queries
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String str=””;
int count=0;
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c == 'A' || c == 'E' || c == 'I' || c == '0' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
continue;
}
str+=c;
}
System.out.print(str);
}
}
import java.util.*;
public class Main
{
public static void main (String[]args)
{
String str=”Gyanendra Tiwari”;
System.out.println(find(str));
}
public static StringBuffer find(String str){
StringBuffer ans=new StringBuffer();
for(int i=0;i<str.length();i++){
char ch=Character.toLowerCase(str.charAt(i)); // convert any character uppercase to lowercase
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){
continue;
}
else{
ans=ans.append(str.charAt(i)); // again using ".charAt()" function because in ch always hold only a lowercase character, not an uppercase.
}
}
return ans;
}
}
import java.util.*;
class Main {
public static void main(String[] args){
String str=”Prepinsta”;
String s1= “aeiouAEIOU”;
String res=””;
first:
for(char c : str.toCharArray()){
second:
for(char i : s1.toCharArray()){
if(c==i){
continue first;
}
}
res= res+c;
continue first; }
System.out.println(res); }
}
import java.util.*;
class Main {
public static void main(String[] args){
String str=”Prepinsta”;
String s1= “aeiouAEIOU”;
String res=””;
first:
for(char c : str.toCharArray()){
second:
for(char i : s1.toCharArray()){
if(c==i){
continue first;
}
}
res= res+c;
continue first;
}
System.out.println(res);
}
}
public class MyClass {
public static void main(String args[]) {
String str=”O somee Singh”;
String f=””;
for(int i=0;i<str.length();i++){
if((str.charAt(i)=='a') || (str.charAt(i)=='e') || (str.charAt(i)=='i') || (str.charAt(i)=='o') ||(str.charAt(i)=='u') && (str.charAt(i)=='A') || (str.charAt(i)=='E') || (str.charAt(i)=='I') || (str.charAt(i)=='O') ||(str.charAt(i)=='U') )
{
continue;
}
f=f+str.charAt(i);
}
System.out.println(f);
}
}
package practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i)== 'o'
|| s.charAt(i) == 'u') {
continue;
} else {
System.out.print(s.charAt(i));
}
}
}
}
import java.util.Scanner;
class aq7 {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String s=sc.next();
String res=””;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)=='a'||s.charAt(i)=='A'){
res=res+"";
}
else if(s.charAt(i)=='e'||s.charAt(i)=='E'){
res=res+"";
}
else if(s.charAt(i)=='i'||s.charAt(i)=='I'){
res=res+"";
}
else if(s.charAt(i)=='o'||s.charAt(i)=='O'){
res=res+"";
}
else if(s.charAt(i)=='u'||s.charAt(i)=='U'){
res=res+"";
}
else{
res=res+s.charAt(i);
}
}
System.out.println(res);
}
}
import java.util.Scanner;
public class vowelcount {
public static void main(String[] args) {
String c=” “;
String vowel=”aeiouAEIOU”;
Scanner sc= new Scanner(System.in);
String s= sc.next();
for( int i=0;i< s.length();i++){
if( vowel.indexOf(s.charAt(i)) ==-1){
c+=s.charAt(i);
}
}
System.out.println(c);
}
}
Good Solution
package stringprogram;
import java.util.Scanner;
public class RemovingVowelsFromStringWithoutAPI
{
//Without Using Api
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println(“Enter String”);
String s = sc.nextLine();
String str=””;
for (int i = 0; i < s.length(); i++)
{
if(s.charAt(i)!='a' && s.charAt(i)!='e' && s.charAt(i)!='i' && s.charAt(i)!='o' &&
s.charAt(i)!='u' && s.charAt(i)!='A' && s.charAt(i)!='E' && s.charAt(i)!='I' &&
s.charAt(i)!='O' && s.charAt(i)!='U')
{
str=str+s.charAt(i);
}
}
System.out.println(" "+str);
}
}
Thank you prepinsta for publishing my code…
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.toLowerCase();
String s = “”;
for(int i=0;i<str.length();i++){
if(str.charAt(i) != 'a' & str.charAt(i) != 'e' & str.charAt(i) != 'i' & str.charAt(i) != 'o'
& str.charAt(i) != 'u'){
s = s + str.charAt(i);
}
}
System.out.print(s);
}
}