ICSE Class 10 Computer Applications Question 22 of 46

String Handling — Question 22

Back to all questions
22
Question

Question 9

Write a program in Java to accept a string and display the number of uppercase, number of lowercase, number of special characters and number of digits present in the string.

import java.util.Scanner;

public class KboatCount
{
   public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = in.nextLine();
             
        int len = str.length();
    
        int uc = 0;
        int lc = 0;
        int sc = 0;
        int dc = 0;
        char ch;

        for (int i = 0; i < len; i++) {
            ch = str.charAt(i);
            if (Character.isUpperCase(ch))  
                uc++;    
            else if (Character.isLowerCase(ch))
                lc++;
            else if (Character.isDigit(ch))
                dc++;
            else if (!Character.isWhitespace(ch))
                sc++;
        }
        
        System.out.println("UpperCase Count = " + uc);
        System.out.println("LowerCase Count = " + lc);
        System.out.println("Digit count = " + dc);
        System.out.println("Special Character Count = " + sc);
        
    }
}
Output
BlueJ output of KboatCount.java
Answer