How to find the vowels in a given string using Java?

Java 8Object Oriented ProgrammingProgramming

You can read a character in a String using the charAt() method. To find the vowels in a given String you need to compare every character in it with the vowel letters.

Example

Live Demo

public class FindingVowels {
   public static void main(String args[]) {

      String str = new String("Hi Welcome to Tutorialspoint");
      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') {
            System.out.println("Given string contains "+str.charAt(i)+" at the index "+i);
         }
      }
   }
}

Output

Given string contains i at the index 1 
Given string contains e at the index 4 
Given string contains o at the index 7 
Given string contains e at the index 9 
Given string contains o at the index 12 
Given string contains u at the index 15 
Given string contains o  at the index 17 
Given string contains i at the index 19 
Given string contains a at the index 20 
Given string contains o at the index 24 
Given string contains i at the index 25
raja
Published on 10-Jan-2018 10:20:12
Advertisements