LAST UPDATED: MARCH 11, 2021
Java Program To Count the Total Number of Vowels and Consonants in a String
In this tutorial, we will learn how to count the total number of vowels and counts in a string. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.
Input: Enter the String: StudyTonight
Output: The total number of vowels is 3
Program 1: Count the Total Number of Vowels and Consonants in a String
In this program, we will see how to count the total number of vowels and counts in a string when the string is pre-defined in the program.
Algorithm
- Start
- Declare a String
- Initialize it.
- Convert the string to a lower case.
- Declare two variables(vcount for vowel counting and ccount for consonant counting) to calculate the vowels and consonants in the string and initialize it to 0.
- Use a for loop to iterate through each character of the string.
- Use an if condition to check whether any character matches with the vowels in the alphabets.
- If any vowel encounters then increment the vcount.
- Else if any consonant encounters then increment the ccount.
- Display the values of both the count variables.
- Stop.
Below is the code for the same in Java language.
//Java Program to find the total number of vowels and consonants with pre-defined values
public class Main
{
public static void main(String []args)
{
String str="Study Tonight"; //Given String
System.out.println("The String is: "+str);
int vcount=0,ccount=0; //Variables to count the vowels and consonants
str=str.toLowerCase(); //Convert the string to lowercase
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
vcount++; //Increment each time vowel encounters
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
ccount++; //Increment each time consonant encounters
}
//Print the total number of vowels
System.out.println("The total number of vowels is: "+vcount);
//Print the total number of consonants
System.out.println("The total number of consonants is: "+ccount);
}
}
The String is: Study Tonight
The total number of vowels is :3
The total number of consonants is:9
Program 2: Count the Total Number of Vowels and Consonants in a String
In this program, we will see how to count the total number of vowels and counts in a string when the string is user-defined. This means, here we will ask the user to initialize the string, and then we will count the total number of vowels and consonants of the entered string.
Algorithm
- Start
- Declare a String
- Ask the user to initialize the string.
- Convert the string to a lower case.
- Declare two variables(vcount for vowel counting and ccount for consonant counting) to calculate the vowels and consonants in the string and initialize it to 0.
- Use a for loop to iterate through each character of the string.
- Use an if condition to check whether any character matches with the vowels in the alphabets.
- If any vowel encounters then increment the vcount.
- Else if any consonant encounters then increment the ccount.
- Display the values of both the count variables.
- Stop.
Below is the code for the same in Java language.
//Java Program to find the total number of vowels and consonants with user-defined values
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String: ");
String str=sc.nextLine(); //Initialize the String
int vcount=0,ccount=0; //Variables to count the vowels and consonants
str=str.toLowerCase(); //Convert the string to lowercase
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
vcount++; //Increment each time vowel encounters
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
ccount++; //Increment each time consonant encounters
}
//Print the total number of vowels
System.out.println("The total number of vowels is: "+vcount);
//Print the total number of consonants
System.out.println("The total number of consonants is: "+ccount);
}
}
Enter the String: Study tonight
The total number of vowels is :3
The total number of consonants is:9