Java Program to Count the Number of Vowels and Consonants in a Sentence
July 28, 2023
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.
Java for Loop:
In Java, the for loop is a control flow statement that allows you to execute a block of code repeatedly for a specific number of times. It has three parts: the initialization expression, the termination condition, and the increment/decrement expression.
The basic Syntax of java for Loop:
for (initialization; termination; increment/decrement) {
// Code to be executed
}
Java if...else Statement:
The if...else statement in Java is a control flow statement that allows you to execute different code blocks depending on the outcome of a condition.
They were introduced in Java 8 and provide a concise way to write functional-style code.
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
}
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
Explanation:
This Java program also counts the number of vowels and consonants in a sentence entered by the user.
It uses regular expressions and the replaceAll() method to remove non-vowel and non-consonant characters from the input string.
The number of vowels and consonants is then determined by counting the remaining characters in the modified string using the length() method.
Finally, the program prints the count of vowels and consonants to the console using System.out.println().
Example 3: Java Program to Check weather an Alphabet is Vowel or Consonant.
Login/Signup to comment