Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

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

  1. Start
  2. Declare a String
  3. Initialize it.
  4. Convert the string to a lower case.
  5. 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.
  6. Use a for loop to iterate through each character of the string.
  7. Use an if condition to check whether any character matches with the vowels in the alphabets.
  8. If any vowel encounters then increment the vcount.
  9. Else if any consonant encounters then increment the ccount.
  10. Display the values of both the count variables.
  11. 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

  1. Start
  2. Declare a String
  3. Ask the user to initialize the string.
  4. Convert the string to a lower case.
  5. 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.
  6. Use a for loop to iterate through each character of the string.
  7. Use an if condition to check whether any character matches with the vowels in the alphabets.
  8. If any vowel encounters then increment the vcount.
  9. Else if any consonant encounters then increment the ccount.
  10. Display the values of both the count variables.
  11. 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