Java Program to Count Number of Vowels in a String

In this article, you will see how to count the number of vowels in a string using Java. A vowel is a sound such as the ones represented in writing by the letters ‘a‘, ‘e‘, ‘i‘, ‘ o‘, and ‘u‘, which you pronounce with your mouth open.

Example-1. Count Vowels in a String using Java

In this program, we check whether the one-by-one extracted single character from the string matches any of the 5 vowels (‘a‘, ‘e‘, ‘i‘, ‘ o‘, and ‘u‘) or not. If yes, then we will increment the vowels count variable. Finally, print the vowel count. let’s see the java program below:

package com.javacodepoint.string;

import java.util.Scanner;

public class CountVowels {

	public static void main(String[] args) {

		// Creating scanner object for reading user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter a string to count Vowels: ");
		String str = sc.next();

		// Variable to count the vowel characters
		int vowelsCount = 0;

		// Iterate over the given string
		for (int i = 0; i < str.length(); i++) {

			// Extract a character from string
			char ch = str.charAt(i);

			// Check Vowel character
			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
					|| ch == 'O' || ch == 'U') {
				vowelsCount++;
			}
		}

		// Print the count
		System.out.println("The total vowels count: " + vowelsCount);

	}

}

OUTPUT:

Enter a string to count Vowels:
Example
The total vowels count: 3

Example-2. Vowels Count using a String with all Vowel Characters.

In this example, we will take a string with all Vowel characters eg: String allVowels=”aeiouAEIOU”, and check whether a character from a given String is available in allVowels or not. if available then increment the count. Let’s see the java program for it below:

package com.javacodepoint.string;

import java.util.Scanner;

public class CountVowels2 {

	public static void main(String[] args) {

		// Creating scanner object for reading user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter a string to count Vowels: ");
		String str = sc.next();

		// Variable to count the total Vowel characters
		int vowelsCount = 0;

		// All Vowels (lowercase and uppercase)
		String allVowels = "aeiouAEIOU";

		// Iterate over the given string
		for (int i = 0; i < str.length(); i++) {

			// Extract a character from string
			char ch = str.charAt(i);

			// Check Vowel character
			if (allVowels.indexOf(ch) != -1) {
				vowelsCount++;
			}
		}

		// Print the count
		System.out.println("The total vowels count: " + vowelsCount);

	}

}

OUTPUT:

Enter a string to count Vowels:
Javacodepoint
The total vowels count: 6

See also:
How to count the occurrence of the given character in a string in java?
How to swap two strings without using the third variable in java?

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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