Computer Science
A string is given as:
Purity of Mind is Essential
Write a program in Java to enter the string. Count and display:
- The character with lowest ASCII code in lower case
- The character with highest ASCII code in lower case
- The character with lowest ASCII code in upper case
- 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
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
Related Questions
Write a program in Java to enter a string in a mixed case. Arrange all the letters of string such that all the lower case characters are followed by the upper case characters.
Sample Input:
Computer ScienceSample Output:
omputercienceCSWrite a program in Java to accept a string. Arrange all the letters of the string in an alphabetical order. Now, insert the missing letters in the sorted string to complete all the letters between first and last characters of the string.
Sample Input:
computerAlphabetical order:
cemoprtuSample Output:
cdefghijklmnopqrstuWrite a program in Java to accept a string and display the new string after reversing the characters of each word.
Sample Input:
Understanding Computer ScienceSample output:
gnidnatsrednU retupmoC ecneicSWrite a program in Java to accept a string. Count and display the frequency of each character present in the string. The character with multiple frequencies should be displayed only once.
Sample Input:
golden jubileeSample Output:
Alphabet g o l d e n j u b i Frequency 1 1 2 1 3 1 1 1 1 1