Count Alphabets, Digits, Special Characters in String using Java

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

Output

Count Alphabets Digits Special Characters in String Java

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