

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
In this article, we will understand how to count the vowels and consonants in Java. Alphabets that include 'a' 'e' 'i' 'o' 'u' are called Vowels and all other alphabets are called Consonants.
Below is a demonstration of the same −
Input
Suppose our input is −
Hello, my name is Charlie
Output
The desired output would be −
The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12
Algorithm
Step1- Start Step 2- Declare two integers: vowels_count, consonants_count and a string my_str Step 3- Prompt the user to enter a string value/ define the string Step 4- Read the values Step 5- Run a for-loop, check each letter whether it is a consonant or an vowel. Increment the respective integer. Store the value. Step 6- Display the result Step 7- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class VowelAndConsonents { public static void main(String[] args) { int vowels_count, consonants_count; String my_str; vowels_count = 0; consonants_count = 0; Scanner scanner = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.print("Enter a statement: "); my_str = scanner.nextLine(); 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
A scanner object has been defined Enter a statement: Hello, my name is Charlie The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
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
- Related Questions & Answers
- Java program to count the number of consonants in a given sentence
- Java program to count the number of vowels in a given sentence
- C# Program to count number of Vowels and Consonants in a string
- How to count number of vowels and consonants in a string in C Language?
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- C Program to count vowels, digits, spaces, consonants using the string concepts
- Frequency of vowels and consonants in JavaScript
- Moving vowels and consonants using JavaScript
- Java Program to count vowels in a string
- Alternating Vowels and Consonants in C/C++
- Validating alternating vowels and consonants in JavaScript
- Java Program to count all vowels in a string
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Python program to count number of vowels using set in a given string
Advertisements