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
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
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
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
Suggested for you
for loop in Java
while loop 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# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…