Java Program to Count Number of Vowels in a String - The Coding Shala

Home >> Java Programs >> Count Number of Vowels in a String

 In this post, we will learn how to Count the Total Number of Vowels in a given String in Java.

Java Program to Count Number of Vowels in a String

You have given a String. Write a Java Program to Count the Total Number of Vowels in the String.

Example 1:
Input: Akshay
Output: 2

Example 2:
Input: This Is a String
Output: 4

Java Program: 

// Java program to Count number of Vowels in String

public class Main {
	
	public static void main(String[] args) { 
		
		String str = "This Is a String";
		int count = 0;
		
		// convert this string to lower case 
		// so we only need to check lower case character
		str = str.toLowerCase();
		
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
				count++;
			}
		}
		
		System.out.println("Number of Vowels in the given string are: " + count);
	}
}

Output: 

Number of Vowels in the given string are: 4


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala