KnowledgeBoat Logo

Computer Applications

Write a program by using class with the following specifications:

Class name — Characters

Data Members:

  1. String str — To store the string

Member Methods:

  1. void input (String st) — to assign st to str
  2. void check_print() — to check and print the following:
    (i) number of letters
    (ii) number of digits
    (iii) number of uppercase characters
    (iv) number of lowercase characters
    (v) number of special characters

Java

Java Classes

24 Likes

Answer

import java.util.Scanner;

public class Characters
{
    private String str;
    
    public void input(String st) {
        str = st;
    }
    
    public void check_print() {
        int cLetters = 0, cDigits = 0, cUpper = 0, cLower = 0,
            cSpecial = 0;
            
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                cLetters++;
                cUpper++;
            }
            else if (ch >= 'a' && ch <= 'z') {
                cLetters++;
                cLower++;
            }
            else if (ch >= '0' && ch <= '9') {
                cDigits++;
            }
            else if (!Character.isWhitespace(ch)) {
                cSpecial++;
            }
        }
        
        System.out.println("Number of Letters: " + cLetters);
        System.out.println("Number of Digits: " + cDigits);
        System.out.println("Number of Upppercase Characters: " 
            + cUpper);
        System.out.println("Number of Lowercase Characters: " 
            + cLower);
        System.out.println("Number of Special Characters: " 
            + cSpecial);
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String s = in.nextLine();
        Characters obj = new Characters();
        obj.input(s);
        obj.check_print();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program by using class with the following specifications: Class name — Characters Data Members: (a) String str — To store the string Member Methods: (b) void input (String st) — to assign st to str (c) void check_print() — to check and print the following: (i) number of letters (ii) number of digits (iii) number of uppercase characters (iv) number of lowercase characters (v) number of special characters

Answered By

10 Likes


Related Questions