
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Count the Number of Vowels and Consonants in a Sentence
To count number of vowels and consonants in a given String, we can use the switch statement and if-else condition in Java. Alphabets that include 'a' 'e' 'i' 'o' 'u' are called Vowels and all other alphabets are called Consonants.
Let's understand the problem statement with an example scenario.
Example Scenario
Suppose our input string is:
Input: Hello, my name is Charlie Output: Vowels = 8, Consonants = 12
In the given string, the collective count of vowels is 8 and consonants is 12.
Using switch Statement
Run a for-loop, check each letter whether it is a consonant or a vowel using switch cases. If the current letter is a vowel, increment its count and store the value. If the current letter is a consonant, increment the consonant count.
Example
In this Java program, we use the switch statement within for loop to count the number of vowels and consonants.
public class VowelAndConsonants { public static void main(String[] args) { int vowels_count = 0, consonants_count = 0; String my_str = "Tutorialspoint"; System.out.println("The statement is defined as: " + my_str); my_str = my_str.toLowerCase(); for (int i = 0; i < my_str.length(); ++i) { char ch = my_str.charAt(i); switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': ++vowels_count; break; default: if (ch >= 'a' && ch <= 'z') { ++consonants_count; } } } System.out.println("The number of vowels in the statement is: " + vowels_count); System.out.println("The number of consonants in the statement is: " + consonants_count); } }
Running the above code will print the count of vowels and consonants.
The statement is defined as: Tutorialspoint The number of vowels in the statement is: 6 The number of consonants in the statement is: 8
Using if-else Condition
Instead of using a switch case, we can pass all vowels as parameters to the if statement. When matching characters are encountered, the count of vowels and consonants will be incremented accordingly.
Example
The following Java program demonstrates the same.
public class VowelAndConsonents { public static void main(String[] args) { int vowels_count, consonants_count; vowels_count = 0; consonants_count = 0; String my_str = "Hello, my name is Charie"; System.out.println("The statement is defined as : " +my_str ); my_str = my_str.toLowerCase(); for (int i = 0; i < my_str.length(); ++i) { char ch = my_str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowels_count; } else if ((ch >= 'a' && ch <= 'z')) { ++consonants_count; } } System.out.println("The number of vowels in the statement is: " + vowels_count); System.out.println("The number of vowels in the Consonants is: " + consonants_count); } }
Output:
The statement is defined as: Hello, my name is Charie The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 11