Hey there! Welcome to ClearUrDoubt.com. In this post, we will look at a Java Program to count the number of vowels in a given text. This can be achieved using “String.toCharArray()” and “switch” statements. Here is the Java Program:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
package com.sample.clearurdoubt; import java.util.Scanner; public class VowelsCounter { public static void main(String[] args) { System.out.println("Enter some text:"); Scanner input = new Scanner(System.in); System.out.println("Tne number of vowels in the given text are : " + vowelsCounter(input.nextLine())); input.close(); } private static int vowelsCounter(String userInput) { int counter = 0; for(char c : userInput.toCharArray()) { switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': counter++; default: continue; } } return counter; } } |
Read more