In this tutorial, we are going to learn java program to count the number of alphabets, digits, and special characters present in an input string from the user.
For any input string, we are going to print the number of alphabets (‘a’-‘z’ and ‘A’-‘Z’), the number of digits(0-9), and the number of special characters present.
For example
Case1: If the user inputs the string ‘%python123%$#’,
the output should be, alphabets=5, digits=3, special characters=4.
Case2: If the user inputs the string ‘!!quescollll151!!%&’,
the output should be, alphabets=10, digits=3, special characters=6.
Java Program to Count Alphabets, Digits, Special Characters in String
Program 1: Using Loops and Character Checks
import java.util.*;
public class Main
{
public static void main (String[]args)
{
int alphabet = 0, digit=0,symbol=0;
System.out.println("Java program to count Alphabet, Digit, Symbols ");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a String");
String str = sc.nextLine();
StringBuilder tempStr = new StringBuilder(str);
for(int i = 0; i < str.length(); i++){
if( (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
alphabet++;
}else if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
digit++;
}else{
symbol++;
}
}
System.out.println ("alphabet = "+alphabet+" Digit = "+ digit+" Symbol = "+symbol);
}
}
Output
Java program to count Alphabet, Digit, Symbols
Please enter a String
Qeusco123!@#
alphabet = 6 Digit = 3 Symbol = 3
Example Explanation
For the input string ‘Que123!@#scol89’, the alphabets used in this string are ‘Q’, ‘u’, ‘e’, ‘s’, ‘w’, ‘e’, ‘l’. The digits used in this string are ‘1’, ‘2’, ‘3’, ‘1’, ‘8’, and ‘9’. And the special character used in this string is ‘!’, ‘@’, and ‘#’.
That makes the number of alphabet =7,
the number of digits = 5
the number of special characters =3
Program Explanation
This Java program counts how many letters, numbers, and symbols are in a text you type. When you start the program, it first tells you that it is going to count alphabets, digits, and symbols. It then asks you to enter a string, which means you can type any mix of letters, numbers, and other characters like punctuation marks. Once you type your string and press enter, the program checks each character one by one. If it finds a character that is a letter (either lower case from ‘a’ to ‘z’ or upper case from ‘A’ to ‘Z’), it adds to the letter count. If it’s a number from ‘0’ to ‘9’, it adds to the number count. All other characters like ‘@’, ‘#’, or ‘!’ add to the symbol count. In the end, the program tells you how many of each type it found by printing the totals. This helps you understand what kinds of characters make up your text.
Program 2: Using Regular Expressions
Regular expressions (regex) can also be used to find matches for specific patterns in a string. You can define patterns for letters, digits, and special characters and count them using the regex API.
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main (String[]args)
{
int letters = 0, digits = 0, specialChars = 0;
System.out.println("Java program to count Alphabet, Digit, Symbols ");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a String");
String str = sc.nextLine();
StringBuilder tempStr = new StringBuilder(str);
Pattern letterPattern = Pattern.compile("[a-zA-Z]");
Pattern digitPattern = Pattern.compile("[0-9]");
Pattern specialCharPattern = Pattern.compile("[^a-zA-Z0-9]");
Matcher letterMatcher = letterPattern.matcher(tempStr);
Matcher digitMatcher = digitPattern.matcher(tempStr);
Matcher specialCharMatcher = specialCharPattern.matcher(tempStr);
while (letterMatcher.find()) letters++;
while (digitMatcher.find()) digits++;
while (specialCharMatcher.find()) specialChars++;
System.out.println("Letters: " + letters);
System.out.println("Digits: " + digits);
System.out.println("Special Characters: " + specialChars);
}
}
Output
Java program to count Alphabet, Digit, Symbols
Please enter a String
@#1234rerEE@#$r
Letters: 6
Digits: 4
Special Characters: 5
Program 3: Using Streams (Java 8+)
For a more modern approach, Java 8’s streams can be used to filter and count characters based on different conditions.
import java.util.*;
public class Main
{
public static void main (String[]args)
{
System.out.println("Java program to count Alphabet, Digit, Symbols ");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a String");
String str = sc.nextLine();
long letters = str.chars()
.filter(Character::isLetter)
.count();
long digits = str.chars()
.filter(Character::isDigit)
.count();
long specialChars = str.chars()
.filter(c -> !Character.isLetterOrDigit(c))
.count();
System.out.println("Letters: " + letters);
System.out.println("Digits: " + digits);
System.out.println("Special Characters: " + specialChars);
}
}
Output
Java program to count Alphabet, Digit, Symbols
Please enter a String
#$@#1234RTYUIO
Letters: 6
Digits: 4
Special Characters: 4