In this article, we will discuss the concept of the Java code: count vowel, consonant, digit, special character and space
In this post, we are going to learn how to count the total number of Vowels, consonants, digits, special character and Space of the given string in Java programming language
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special characters and Spaces of the given string using for loop in Java programing language
program 1
//program to count vowel,consonant,digit,special character and space import java.util.Scanner; public class CountVowelconsdigitspecialspace{ public static void main(String args[]){ //variable declaration String str; int vCount=0,cCount=0,digits=0,spaces=0,spe_Char=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); for(int i=0; i<=str.length()-1; i++){ char ch=str.charAt(i); if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' // check vowels ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){ vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){ //check consonants cCount++; } else if(ch>='0' && ch<='9') // check digits { digits++; } else if(ch==' '){ // check spaces spaces++; } else{ spe_Char++; } } System.out.println("\nTotal Vowels: "+vCount);//display total number of vowels System.out.println("Total consonants: "+cCount);// display total number of consoanats System.out.println("\nTotal digits: "+digits);// display total number of digits System.out.println("Total white space : "+spaces); // display total number of spaces System.out.println("Total special characters: "+(spe_Char));// display total number of special characters } }
When the above code is executed, it produces the following result
Enter the String Code4coding.com is a programming website Total vowels: 13 Total consonants: 21 Total digits: 1 Total white space: 4 Total special characters:1
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special character and Space of the given string using while loop in Java programing language
Program 2
//program to count vowel,consonant,digit,special character and space import java.util.Scanner; public class CountVowelconsdigitspecialspace1{ public static void main(String args[]){ //variable declaration String str; int vCount=0,cCount=0,digits=0,spaces=0,spe_Char=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); int i=0; while(i<=str.length()-1){ 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' ){ // check vowels vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){ // check consonants cCount++; } else if(ch>='0' && ch<='9') // check digits { digits++; } else if(ch==' '){ // check space spaces++; } else{ spe_Char++; } i++; } System.out.println("\nTotal Vowels: "+vCount); //display total number of vowels System.out.println("Total consonants: "+cCount); //display total number of consonants System.out.println("\nTotal digits: "+digits); //display total number of digits System.out.println("Total white space : "+spaces); //display total number of spacess System.out.println("Total special characters: "+(spe_Char)); //display total number of special characters } }
When the above code is executed, it produces the following result
Enter the String Code4coding.com coding 2_0_2_0 Total vowels: 7 Total consonants: 12 Total digits: 5 Total white space: 2 Total special characters:4
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special character and Space of the given string using do-while loop in Java programing language
Program 3
//program to count vowel,consonant,digit,special character and space import java.util.Scanner; public class CountVowelconsdigitspecialspace2{ public static void main(String args[]){ //variable declaration String str; int vCount=0,cCount=0,digits=0,spaces=0,spe_Char=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); int i=0; do{ char ch=str.charAt(i);//check vowels if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){ vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){// ceck consonant cCount++; } else if(ch>='0' && ch<='9') // check digits { digits++; } else if(ch==' '){ //check space spaces++; } else{ spe_Char++; } i++; }while(i<=str.length()-1); System.out.println("\nTotal Vowels: "+vCount); //display vowels System.out.println("Total consonants: "+cCount); // display consonant System.out.println("\nTotal digits: "+digits); // display digits System.out.println("Total white space : "+spaces); // display space System.out.println("Total special characters: "+(spe_Char));// display special characetrs } }
When the above code is executed, it produces the following result
Enter the String Code_4_coding.com web portal Total vowels: 8 Total consonants: 14 Total digits: 1 Total white space: 2 Total special characters:3
Approaches
Suggested for you
for loop in Java language
while loop in Java language
do-while loop in Java language
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
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…