Computer Science

A string is given as:
Purity of Mind is Essential

Write a program in Java to enter the string. Count and display:

  1. The character with lowest ASCII code in lower case
  2. The character with highest ASCII code in lower case
  3. The character with lowest ASCII code in upper case
  4. The character with highest ASCII code in upper case

Sample Output:

The character with lowest ASCII code in lower case: a
The character with highest ASCII code in lower case: y
The character with lowest ASCII code in upper case: E
The character with highest ASCII code in upper case: P

Java

Java String Handling

30 Likes

Answer

import java.util.Scanner;

public class KboatASCIICode
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string:");
        String str = in.nextLine();
        int len = str.length();
        
        /*
         * Intialize low variables to
         * highest ASCII code and
         * high variables to lowest
         * ASCII code
         */
        char lowerCaseLow = 255;
        char lowerCaseHigh = 0;
        char upperCaseLow = 255;
        char upperCaseHigh = 0;
        
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (Character.isLowerCase(ch)) {
                if (ch < lowerCaseLow) {
                    lowerCaseLow = ch;
                }
                
                if (ch > lowerCaseHigh) {
                    lowerCaseHigh = ch;
                }
            }
            else if (Character.isUpperCase(ch)) {
                if (ch < upperCaseLow) {
                    upperCaseLow = ch;
                }
                
                if (ch > upperCaseHigh) {
                    upperCaseHigh = ch;
                }
            }
        }
        
        System.out.println("The character with lowest ASCII code in lower case: " 
                                + lowerCaseLow);
        System.out.println("The character with highest ASCII code in lower case: " 
                                + lowerCaseHigh);
        System.out.println("The character with lowest ASCII code in upper case: " 
                                + upperCaseLow);
        System.out.println("The character with highest ASCII code in upper case: " 
                                + upperCaseHigh);
    }
}

Output

Answered By

9 Likes


Related Questions