Count Number of Vowels in any string
Here you will learn that how to make a java program to count number of vowels in a string using string length method.
There are 5 vowels in the English alphabet that’s are a,e,i,o,u. let’s do this program by using java programming.
Java Program to Count Number of Vowels in a String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.*; public class CountVowels { public static void main(String []args) { String str; int k,ct=0; Scanner in = new Scanner(System.in); System.out.print("Enter a sentence : "); str = in.nextLine(); for(k=0;k<str.length();k++) { if(str.charAt(k) == 'a' || str.charAt(k) == 'e' || str.charAt(k) == 'i' || str.charAt(k) == 'o' || str.charAt(k) == 'u' || str.charAt(k) == 'A' || str.charAt(k) == 'E' || str.charAt(k) == 'I' || str.charAt(k) == 'O' || str.charAt(k) == 'U') { ct++; } } System.out.println("Total Vowels are :"+ct); } } |
Output
Check out our other Java Programming Examples