Java Program to Count Number of Vowels in a String - The Coding Shala
Home >> Java Programs >> Count Number of Vowels in a String
Other Posts You May Like
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
- Java Program to Reverse a String
- Java Program to Reverse a String using Recursion
- Java Program to Find the Duplicate Characters Count in a String
- Java Program to Compare Two Strings without Whitespaces
- Java Program to Count Negative Numbers in a Sorted Matrix
Comments
Post a Comment