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.
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 }
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
Explanation:
This Java code counts the number of vowels and consonants in a sentence entered by the user.
It uses a Scanner object to read input from the user and a for loop to iterate through each character in the sentence.
The program checks each character to see if it is a vowel or consonant, and increments the corresponding counter.
After the loop completes, the program prints the number of vowels and consonants found in the sentence.
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
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.
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
Explanation:
It prompts the user to enter a sentence using the Scanner class.
It initializes variables vowelCount and consonantCount to 0, and sets the sentence to lowercase.
It loops through each character in the sentence and checks if it is a vowel or consonant using an if-else statement.
If the character is a vowel, the vowelCount variable is incremented. If the character is a consonant, the consonantCount variable is incremented.
After the loop is done, the program prints out the number of vowels and consonants in the sentence.
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
Login/Signup to comment