Alphabet

Java code: count vowel,consonant,digit,special character and space

Java code: count vowel,consonant,digit,special character and space

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

count vowel, consonant, digit, special character and space

Code to count Vowels, consonants, digits special characters and space of a string

Count vowels, consonants, digits, special characters and space using for loop

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

Count vowel, consonant, digit, special character and space using while loop

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

 

Count vowel, consonant, digit, special character and space using do-while loop

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

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as vCount=0,cCount=0,digits=0,spaces=0,spe_Char=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A loop(for, while and do-while) is used to count every total of the given string.
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test vowels, when the if statements is true,  The vCount becomes vCount+ 1( vCount= vCount +1);
  8. When it is false, control moves to the else-if (second condition) and evaluates the test-expression of else-if. if it is true The cCount becomes cCount+1(cCount=cCount+1);
  9. When it is false, control moves to the next else-if part(third condition) and evaluates the test-expression of else-if. if it is true The digits becomes digits+1(digits=digits+1);
  10. When it is false, control moves to the next else-if part(fourth condition) and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  11. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  12. Finally, the program displays the total number of the vowels, consonants, digits, Special character and Space of the given string

 

 

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

 

 

C program: Count word,characters and space of a string
Cpp code: count vowel,consonant,digit,special character and space
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago