Find Vowels in a String

In this post, we will see how to find and count vowels in a string.
[toc]

Find Vowels in a String

If any character in String satisfy below condition then it is vowel and we will add it to Hashset.

character==’a’ || character==’A’ || character==’e’ || character==’E’ ||
character==’i’ || character==’I’ || character==’o’ || character==’O’ ||
character==’u’ || character==’U’
package org.arpit.java2blog;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class VowelFinder
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an String : ");
        String str = scanner.next();

        Set<Character> set=new HashSet<Character>();
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if(isVowel(c))
            {
                set.add(c);
            }
        }

        System.out.println("Vowels are:");
        for (Character c:set) {
            System.out.print(" "+c);
        }

        scanner.close();
    }

    public static boolean isVowel(char character)
    {

        if(character=='a' || character=='A' || character=='e' || character=='E' ||
                character=='i' || character=='I' || character=='o' || character=='O' ||
                character=='u' || character=='U'){
            return true;
        }else{
            return false;
        }
    }

}

Output:

Enter an String : Java2blog
Vowels are:
a o

Explanation

  1. Iterate over String str and check each Character is vowel or not based on below condition
    if(character=='a' || character=='A' || character=='e' || character=='E' ||
                        character=='i' || character=='I' || character=='o' || character=='O' ||
                        character=='u' || character=='U')
    
  2. If it is vowel, then print the vowel

Count number of Vowels in the String

Here is the program to count number of Vowels in the String.

package org.arpit.java2blog;

import java.util.Scanner;

public class VowelCounter
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an String : ");
        String str = scanner.next();

        int countVowels = countVowels(str);
        System.out.println("Number of vowels: "+countVowels);
        scanner.close();
    }

    public static int countVowels(String str)
    {
        int count = 0;

        for (int i = 0; i < str.length(); i++) {
            char character =str.charAt(i);
            if(character=='a' || character=='A' || character=='e' || character=='E' ||
                    character=='i' || character=='I' || character=='o' || character=='O' ||
                    character=='u' || character=='U'){
                count++;
            }
        }
        return count;
    }
}

Output:

Enter an String : Java2blog
Number of vowels: 3

Explanation

  1. Declare count variable and initialize it with 0.
  2. Iterate over String str and check each Character is vowel or not based on below condition
    if(character=='a' || character=='A' || character=='e' || character=='E' ||
                        character=='i' || character=='I' || character=='o' || character=='O' ||
                        character=='u' || character=='U')
    
  3. If it is vowel, then increment the count
  4. Once loop is complete, then return the count.

That’s all about how to find vowels in a string in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.