String

Java code to Count Alphabets, Numeric, Special character and Space

Java code to Count Alphabets, Numeric, Special character and Space

In this article, we will discuss the concept of the Java code to Count Alphabets, Numeric, Special character and Space

In this post, we are going to learn how to count Alphabets, Numeric, Special character and space of the given String in Java programming language

Count Alphabets, Numeric, Special character and Space

Java code to Count Alphabets, Numeric, Special character and Space

Program to count Alphabets, Numeric, Special character and Space using for loop

The program allows the user to enter a String and then it counts and display the total number of  Alphabets, Numeric, Special character and Space of the given string using for loop in Java programing language

Program 1

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountAlphabetsNumOthers{
public static void main(String args[]){
//variable declaration
    String str;
int Alphabets=0,num=0,space=0,Special=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(); i++){
char ch=str.charAt(i);
if(ch>='A' && ch<='Z' || ch>='a' && ch<='z'){ //Check Alphabets
    Alphabets++;
}

else if(ch>='0' && ch<='9'){ //Check numeric characters
    num++;
}
else if(ch ==' '){ //check space
    space++;
}
else{   //find the special caracters
    Special++;
}

}
System.out.println("\nAlphabet letters: "+Alphabets);
System.out.println("Number: "+num);
System.out.println("Space: "+space);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces te following result

Enter the string
Code For Coding:code4coding.com

Alphabet letters: 26
Numbers: 1
Space: 2
Special characters: 2

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop 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 if(ch>=’A’ && ch<=’Z’ || ch>=’a’ && ch<=’z’), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string

 

Program to count Alphabets, Numeric, 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  Alphabets, Numeric, Special character and Space of the given string using while loop in Java programing language

Program 2

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountAlphabetsNumOthers1{
public static void main(String args[]){
//variable declaration
    String str;
int Alphabets=0,num=0,space=0,Special=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()){
char ch=str.charAt(i);
if(ch>='A' && ch<='Z' || ch>='a' && ch<='z'){ //Check Alphabets
    Alphabets++;
}

else if(ch>='0' && ch<='9'){ //Check numeric charaters
    num++;
}
else if(ch ==' '){
    space++; //Check Space
}
else{
    Special++; //Find special characters
}
i++;
}
System.out.println("\nAlphabet letters: "+Alphabets);
System.out.println("Number: "+num);
System.out.println("Space: "+space);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces te following result

Enter the string
C programming solution:code4coding.com


Alphabet letters: 32
Numbers: 1
Space: 2
Special characters: 2

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A while loop 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 if(ch>=’A’ && ch<=’Z’ || ch>=’a’ && ch<=’z’), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string

 

Program to count Alphabets, Numeric, 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  Alphabets, Numeric, Special character and Space of the given string using do-wile loop in Java programing language

Program 3

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountAlphabetsNumOthers2{
public static void main(String args[]){
//variable declaration
    String str;
int Alphabets=0,num=0,space=0,Special=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);
if(ch>='A' && ch<='Z' || ch>='a' && ch<='z'){ //Ceck Alphabets
    Alphabets++;
}

else if(ch>='0' && ch<='9'){ //Check  numeric characters
    num++;
}
else if(ch ==' '){ //Check space
    space++;
}
else{ //find special characters
    Special++;
}
i++;
}while(i<str.length());
System.out.println("\nAlphabet letters: "+Alphabets);
System.out.println("Number: "+num);
System.out.println("Space: "+space);
System.out.println("Special characters: "+Special);
}
}

When the above code is executed, it produces te following result

Enter the string
Java program:code4coding.com

Alphabet letters: 24
Numbers: 1
Space: 1
Special characters: 2

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A do-while loop 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 if(ch>=’A’ && ch<=’Z’ || ch>=’a’ && ch<=’z’), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, 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

 

Code to Count Alphabets, Numeric, Special character and Space in a string in Python
C++ code to Count Alphabets, Numeric, Special character and Space in a string
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.

View Comments

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago