Java Program to Count the Number of Vowels and Consonants in a Sentence

JavaObject Oriented ProgrammingProgramming

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 run button.

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
raja
Updated on 21-Feb-2022 11:34:59

Advertisements