Open In App

Java Program to Count Number of Vowels in a String

In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io package in java provides input and output through data streams, serialization, and the file system.

We can count the vowels in a string in two ways:



  1. Iterative
  2. Recursive

Examples:

Input: GeeksForGeeks
Output: Total no of vowels in string are: 5

Input: ChETaN
Output: Total no of vowels in string are: 2

Method 1: Iterative 
Approach:



Below is the implementation of the above approach:




// Java Program to Count Number of Vowels
// in a String in a iterative way
 
import java.io.*;
 
public class vowel {
    public static void main(String[] args)
        throws IOException
    {
        String str = "GeeksForGeeks";
        str = str.toLowerCase();
        int count = 0;
 
        for (int i = 0; i < str.length(); i++) {
            // check if char[i] is vowel
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e'
                || str.charAt(i) == 'i'
                || str.charAt(i) == 'o'
                || str.charAt(i) == 'u') {
                // count increments if there is vowel in
                // char[i]
                count++;
            }
        }
 
        // display total count of vowels in string
        System.out.println(
            "Total no of vowels in string are: " + count);
    }
}

Output
Total no of vowels in string are: 5

Method 2: Recursive
Approach:

Below is the implementation of the above approach:




// Java Program to Count Number of Vowels
// in a String in a recursive way
 
import java.io.*;
 
class GFG {
 
    // isVowel() function returns 1 if the
    // character is a vowel and 0 if it is not
    static int isVowel(char chars)
    {
        if (chars == 'a' || chars == 'e' || chars == 'i'
            || chars == 'o' || chars == 'u') {
            return 1;
        }
        else {
            return 0;
        }
    }
 
    // recursive function to return the number
    // of characters in a string
    static int vowelno(String str, int l)
    {
        if (l == 1) {
            return isVowel(str.charAt(l - 1));
        }
 
        return vowelno(str, l - 1)
            + isVowel(str.charAt(l - 1));
    }
 
    public static void main(String[] args)
        throws IOException
    {
        String str = "BufferedOutput";
 
        str = str.toLowerCase();
 
        System.out.println(
            "Total number of vowels in string are:");
 
        System.out.println(vowelno(str, str.length()));
    }
}

Output
Total number of vowels in string are:
6

Method 3: Using ArrayList and contains() method




// Java Program to Count Number of Vowels
// in a String
 
import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws IOException
    {
        String str = "GeeksForGeeks";
        str = str.toLowerCase();
        int count = 0;
        String vow ="aeiou";
        ArrayList<Character> vowels = new ArrayList<Character>();
        for(int i=0;i<vow.length();i++)
        {
            vowels.add(vow.charAt(i));
        }
        for (int i = 0; i < str.length(); i++) {
            if(vowels.contains(str.charAt(i))){
                count++;
            }
        }
 
        // display total count of vowels in string
        System.out.println("Total no of vowels in string are: " + count);
    }
}

Output
Total no of vowels in string are: 5

Article Tags :