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
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
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
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
Suggested for you
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
View Comments
For program 2, output of Alphabet letters: is 33 as per my output. Thanks.