Java Program to remove Vowels from a String

Source code of Java Program to remove vowels from a given string

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class RemoveVowels {

   public static void main(String args[]) throws IOException
   {
     BufferedReader in = new BufferedReader
         (new InputStreamReader(System.in));

     System.out.println("Program to remove vowels ");
     System.out.println("from a given string \n");

     System.out.print("Enter a string: ");
     String s = in.readLine();

     System.out.println("\n"+"String with vowels removed: ");
     String r = removeVowels(s);
     System.out.println(r);
   }

   private static String removeVowels(String s)
   {
     String finalString = "";

     for (int i=0; i<s.length(); i++)
     {
       if (!isVowel
           (Character.toLowerCase
               (s.charAt(i))))
       {
         finalString += s.charAt(i);
       }
     }
     return finalString;
   }

   private static boolean isVowel(char c) {

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

Output of Java Program for removing Vowels from a string

0 comments:

Post a Comment