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

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

Java if…else Statement

  • The if…else statement in Java is a conditional statement that lets you run several blocks of code in response to whether a particular condition is true or not.
  • You can also use nested if…else statements to handle more complex conditions.

    • You must be familiar with following topics to understand the correspond example Such as: Java if… else statement and Java For Loop.

  • To understand the how to Check weather an Alphabet is Vowel or Consonant, Read the Complete Article.

Steps to Count the Number of Vowels and Consonants in a Sentence

  • Here are the steps to count the number of vowels and consonants in a sentence.
    • Prompt the user to enter a sentence.
    • Initialize variables to keep track of the number of vowels and consonants in the sentence.
    • Convert the sentence to lowercase (or uppercase) so that it’s easier to check for vowels and consonants.
    • Loop through each character in the sentence.
    • Check if the character is a vowel or consonant. You can do this using an if-else statement, or by using the switch statement in Java.
    • If the character is a vowel, increment the vowel count variable. If the character is a consonant, increment the consonant count variable.
    • After the loop is done, print out the number of vowels and consonants in the sentence.

The basic Syntax of java for Loop:

for (initialization; termination; increment/decrement) {
    // Code to be executed
}

The basic syntax for if…else statement is:

if (condition) {
    // statements to execute if the condition is true
} else {
    // statements to execute if the condition is false
}

Let’s look at the Java Program to Check weather an Alphabet is Vowel or Consonant to perform certain operations.

Example 1: Java Program to Check weather an Alphabet is Vowel or Consonant

Run
import java.util.Scanner;

public class Main
{
  public static void main (String[]args)
  {
    Scanner scanner = new Scanner (System.in);
      System.out.println ("Enter a sentence: ");
    String sentence = scanner.nextLine ().toLowerCase ();

    int vowelCount = 0;
    int consonantCount = 0;

    for (int i = 0; i < sentence.length (); i++)
      {
	char ch = sentence.charAt (i);

	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
	  {
	    vowelCount++;
	  }
	else if (ch >= 'a' && ch <= 'z')
	  {
	    consonantCount++;
	  }
      }

    System.out.println ("Number of vowels: " + vowelCount);
    System.out.println ("Number of consonants: " + consonantCount);
  }
}

Output

Enter a sentence: 
PrepInstaorprePrepInstaPrime
Number of vowels: 9
Number of consonants: 16

Example 2 :Java Program to Check weather an Alphabet is Vowel or Consonant

Run
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a statement: ");
        String sentence = scanner.nextLine().toLowerCase();

        int vowelCount = sentence.replaceAll("[^aeiou]", "").length();
        int consonantCount = sentence.replaceAll("[^bcdfghjklmnpqrstvwxyz]", "").length();

        System.out.println("Number of vowels: " + vowelCount);
        System.out.println("Number of consonants: " + consonantCount);
    }
}

Output

Enter a statement: 
Wecome To PrepInsta
Number of vowels: 7
Number of consonants: 10

Example 3: Java Program to Check weather an Alphabet is Vowel or Consonant.

Run
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String sentence = scanner.nextLine();
        scanner.close();
        
        int vowelCount = 0;
        int consonantCount = 0;
        sentence = sentence.toLowerCase();
        
        for (int i = 0; i < sentence.length(); i++) {
            char ch = sentence.charAt(i);
            
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowelCount++;
            } else if (ch >= 'a' && ch <= 'z') {
                consonantCount++;
            }
        }
        
        System.out.println("Number of vowels: " + vowelCount);
        System.out.println("Number of consonants: " + consonantCount);
    }
}

Output

Enter a sentence: My Name is Ayush
Number of vowels: 5
Number of consonants: 8

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription