Table of Contents
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 write a program to count total number of vowels and consonants in given string input from user in Java language
Code to count Vowels or consonants in a string
Code to count number of Vowel and consonant in a string, using for loop – #1
In this code, we are going to learn how to count the total number of Vowel and consonant in the given String using for loop in Java language
Program 1
import java.util.Scanner;
public class CountVowelsAndConsonants{
public static void main(String args[]){
String str;//declare a string
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 ");
//Ask input from uer as string
str=scan.nextLine();//reading the input
int i=0;
for(i=0; i<str.length(); i++){
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++;
}
}
System.out.println("Number of vowels: "+vowCount);
//display number of vowels
System.out.println("Number of consonant: "+consCount);
//display number of consonants
}
}
When the above code is executed, it produces the following result
Enter the String for count vowel and consonant My Java Number of Vowels: 2 Number of consonant: 4
Code to count number of Vowel and consonant in a string, using while loop – #2
In this code, we are going to learn how to count the total number of Vowel and consonant in the given string using while loop in Java language
Program 2
import java.util.Scanner;
public class CountVowelsAndConsonants1{
public static void main(String args[]){
String str;//declare character variable
int vowCount=0,consCount=0;
//initiolize variables vowCount and consCount 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 ");
//Ask input from user as string
str=scan.nextLine();//reading the string input
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++;
}
else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
consCount++;
}
i++;
}
System.out.println("Num ber of vowels: "+vowCount);
//display number of vowels in the given string
System.out.println("Num ber of consonant: "+consCount);
//display number of consonant in the given string
}
}
When the above code is executed, it produces the following result
Enter the String for count vowel and consonant Programming Number of Vowels: 3 Number of consonant: 8
Code to count number of Vowel and consonant in a string, using do-while loop – #3
In this code, we are going to learn how to count the total number of Vowel and consonant in the given string using do-while loop in Java language
Program 3
import java.util.Scanner;
public class CountVowelsAndConsonantsAscii{
public static void main(String args[]){
String str;//declare a string variable
int vowCount=0,consCount=0;
//initialize 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=0;
for(i=0; i<str.length(); i++){
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++;
}
}
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
Suggested post
Nested if statements in Java language
Do-while loop in Java language
Similar post
Java program to print all alphabets in given range
C program to print all alphabets in given range
C++ program to print all alphabets in given range
Java program to print all alphabets using loops
C program to print all alphabets using loops
C++ program to print all alphabets using loops
Java program to check whether a character is Alphabet or Not
C++ program to check whether a character is Alphabet or Not
C program to check whether a character is Alphabet or Not
Python program to check whether a character is Alphabet or Not