Alphabet

Program to count vowel and consonant of a string in Java using ASCII value

Program to count vowel and consonant of a string in Java using ASCII value

In this article, we will discuss the concept of Program to count vowel and consonant of a string in Java using ASCII value

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

count vowels and consonant

Find vowel and consonant of a string in Java using ASCII value

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 CountVowelAndConsonantAscii{
public static void main(String args[]){
    String str;//declare a string variable
int vowCount=0,consCount=0;
//declare a counter variable

 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; 
for(i=0; i<str.length(); i++){
    char ch=str.charAt(i);
    if(ch == 97|| ch == 101|| ch == 105|| ch == 111|| ch == 117  //using ASCII value
    ||ch == 65|| ch == 69|| ch == 73|| ch == 79|| ch == 85 ){
        vowCount++;//vowel counter is incremented by 1
    }
    else if((ch >= 97 && ch <= 122 || ch >= 65 && ch <= 90 )){
        consCount++;//vowel counter is incremented by 1
    }
}
System.out.println("The number of vowels: "+vowCount);
System.out.println("The number of consonants: "+consCount);
}
}

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

Enter the String for count vowel and consonant
Java programming
The number of vowels: 5
The number of consonants: 10


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

import java.util.Scanner;
public class CountVowelAndConsonantAscii1{
public static void main(String args[]){
    String str;
int vowCount=0,consCount=0;//declare a counter variable and initialized as zero 

 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 == 97|| ch == 101|| ch == 105|| ch == 111|| ch == 117
    ||ch == 65|| ch == 69|| ch == 73|| ch == 79|| ch == 85 ){
        vowCount++;
    }
    else if((ch >= 97 && ch <= 122 || ch >= 65 && ch <= 90 )){
        consCount++;
    }
     i++;
}
System.out.println("The number of vowels: "+vowCount);
System.out.println("The number of consonant: "+consCount);
}
}

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

Enter the String for count vowel and consonant
Java programming language
The number of vowels: 9
The number of consonants: 14

 

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 CountVowelAndConsonantAscii2{
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 == 97|| ch == 101|| ch == 105|| ch == 111|| ch == 117
    ||ch == 65|| ch == 69|| ch == 73|| ch == 79|| ch == 85 ){
        vowCount++;
    }
    else if((ch >= 97 && ch <= 122 || ch >= 65 && ch <= 90 )){
        consCount++;
    }
     i++;
}while(i<str.length());
System.out.println("The number of vowels: "+vowCount);
System.out.println("The number of consonant: "+consCount);
}
}

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

Enter the String for count vowel and consonant
Vowels and consonants
The number of vowels: 6
The number of consonants: 13

 

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

Program to count vowel and consonant of a string inC++ using ASCII value

Program to count vowel and consonant of a string in C using ASCII value

 

C++ program to count vowels and consonants in a string
Program for count vowel and consonant of a string in C++ using ASCII value
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.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago