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
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
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
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
Suggested for you
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
View Comments
How will it be for while to just count vowels and not consonants?