← All resources

Write a program to input a string and print the number of capital letters, small letters, digits and special characters in it.

import java.util.*;
public class Count_Chars
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        String s;
        char ch;
        int Cl=0, Sl=0, Dg=0, Sc=0, i, l;
        System.out.println("Enter a string:");
        s = sc.nextLine();
        l = s.length();
        for(i=0;i<l;i++)
        {
            ch = s.charAt(i);
            if(ch>='A'&&ch<='Z')
            Cl=Cl+1;
            else if(ch>='a'&&ch<='z')
            Sl = Sl+1;
            else if(ch>='0'&&ch<='9')
            Dg = Dg+1;
            else
            Sc = Sc+1;
        }
        System.out.println("The number of Capital Letters are: "+Cl);
        System.out.println("The number of Small Letters are: "+Sl);
        System.out.println("The number of Digits are: "+Dg);
        System.out.println("The number of Special Characters are: "+Sc);
    }
}