Alphabet

Java program to count vowels and consonants in a string

Java program to count vowels and consonants in a string

In this article, we will discuss the concept of Java program to count vowels and consonants in a string

In this post, we are going to learn how to count the vowels and consonants in the given string in Java  programming language

count vowels and consonant

Code to count the vowels and consonants  using for loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using for loop in Java  language

Program 1

import java.util.Scanner;
public class CountVowelAndConsonant{
public static void main(String args[]){
    String str;
int vowCount=0,consCount=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String for count vowel and consonant  ");
str=scan.nextLine();
int i=0; 
for(i=0; i<str.length(); i++){
    char ch=str.charAt(i);
//check whether a character is a vowel
    if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u'
    ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){
        vowCount++;//increments the vowel counter
    }
//check whether a character is a consonant
    else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
        consCount++;//increments the consonant counter
    }
}
System.out.println("Number of vowels: "+vowCount);//display vowels
System.out.println("Number of consonants: "+consCount);//display consonant
}
}

When the above code is executed, it produces the following result

Enter the String for count vowel and consonant
Alphabets
Number of vowels: 3
Number of consonants: 6


Approach

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter-variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A for-loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true,  vowCount becomes vowCount + 1    (vowCount=vowCount+1)
  7. When the if-statements is false, The control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  consCount becomes consCount + 1(consCount =consCount +1)
  9. If the test expression of else if is false, control terminates from the loop
  10. Finally, the program displays the number of vowels and consonants of the given string.

 

Code to count the vowels and consonants using while loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using while loop in Java  language

Program 2

import java.util.Scanner;
public class CountVowelAndConsonant1{
public static void main(String args[]){
    String str;
int vowCount=0,consCount=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String for count vowel and consonant  ");
str=scan.nextLine();
int i=0; 
while(i<str.length()){
    char ch=str.charAt(i);
    if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u'
    ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){
        vowCount++;//increment the vowels counter
    }
    else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
        consCount++;//increment the consonants counter
    }
     i++;
}
System.out.println("Num ber of vowels: "+vowCount);//display the vowels
System.out.println("Num ber of consonant: "+consCount);//display the consonant
}
}

When the above code is executed, it produces the following result

Enter the String for count vowel and consonant
String
Number of vowels: 1
Number of consonants: 5


Approach

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A while-loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true,  vowCount becomes vowCount + 1(vowCount=vowCount+1)
  7. When the if-statement is false, control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  consCount becomes consCount + 1(consCount =consCount +1)
  9. If the test expression of else if is false, control skips from the loop
  10. Finally, the program displays the number of vowels and consonants of the given string.

 

Code to count the vowels and consonants using do-while loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using do-while loop in Java  language

Program 3

import java.util.Scanner;
public class CountVowelAndConsonant2{
public static void main(String args[]){
    String str;
int vowCount=0,consCount=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String for count vowel and consonant  ");
str=scan.nextLine();
int i=0; 
do{
    char ch=str.charAt(i);
    if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u'
    ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){
        vowCount++;
    }
    else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
        consCount++;
    }
     i++;
}while(i<str.length());
System.out.println("Num ber of vowels: "+vowCount);
System.out.println("Num ber of consonant: "+consCount);
}
}

When the above code is executed, it produces the following result

Enter the String for count vowel and consonant
Consonants
Number of vowels: 3
Number of consonants: 7


Approach

  1. Declare a variable as String str;
  2. Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A do-while loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true,  vowCount becomes vowCount + 1(vowCount=vowCount+1)
  7. When the if-statement is false, control moves to else if and checks the test expression of else-if
  8. If the test expression of else-if is true,  consCount becomes consCount + 1(consCount =consCount +1)
  9. If the test expression of else if is false, control skips from the loop
  10. Finally, the program displays the number of vowels and consonants of the given string.

 

Suggested for you

for loop in Java

while loop in Java

do-wile loop in Java

if statements in Java

Operator in Java

 

Similar post

C code to check whether the Alphabet is vowel or consonant

C++ code to check whether the Alphabet is vowel or consonant

Python  code to check whether the Alphabet is vowel or consonant

Java code to check whether the Alphabet is vowel or consonant

 

C++ program to count vowels and consonants in a string

C program to count vowels and consonants in a string

Python program to count vowels and consonants in a string

 

Python program for display all Alphabets using ASCII value
C program to count vowels and consonants in a string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago