Table of Contents
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.