Table of Contents
Java program to count the letters, spaces, numbers and other characters
Write a Java program to count the letters, spaces, numbers and other characters of
an input string : This simple and easy Java program will even count uppercase and lowercase letters.
Java program to count the letters spaces numbers and other characters
import java.util.*;
public class Demo {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter any string : ");
String msg = in.nextLine();
char[] ch = msg.toCharArray();
int ltr, spc, num, sch, ucl, lcl;
ltr = spc = num = sch = ucl = lcl = 0;
for(int i = 0; i < msg.length(); i++)
{
if(Character.isLetter(ch[i]))
{
ltr++ ;
if(Character.isLowerCase(ch[i]))
{
lcl++;
}
if(Character.isUpperCase(ch[i]))
{
ucl++;
}
}
else if(Character.isDigit(ch[i]))
{
num++ ;
}
else if(Character.isSpaceChar(ch[i]))
{
spc++ ;
}
else
{
sch++;
}
}
System.out.println("Letter : " + ltr);
System.out.println("Upper Case Letter : " + ucl);
System.out.println("Lower Case Letter : " + lcl);
System.out.println("Space : " + spc);
System.out.println("Numbers : " + num);
System.out.println("Other : " + sch);
}
}
Output

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author