Write a program that reads a string from the keyboard and computes the two array
ID: 3911881 • Letter: W
Question
Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format:
The frequency of the letter A=
The frequency of the letter B=
Assume the input string contains only uppercase and lower case letters. Your solution should be efficient and it does not use a sequence of 26 IF statements or a Switch statement.
You may need the casting operation to convert int to char and vice versa.
Explanation / Answer
Java Code for the given requirement:
import java.util.Scanner;
public class Assignment {
public static void main(String [] args){
//Getting the input String from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter input String: ");
String input = scan.nextLine();
//creating two arrays
int lowerCase[]=new int[26];
int upperCase[]=new int[26];
//using for loops for counting each character
for(char ch = 'a'; ch<='z'; ch++){
lowerCase[(int)ch-97] = charCount(ch,input) ;
}
for(char ch = 'A'; ch<='Z'; ch++){
upperCase[(int)ch-65] = charCount(ch,input) ;
}
System.out.println("Lower Case characters");
for(int i =0; i<lowerCase.length; i++){
System.out.println("The frequency of the letter "+(char) (i+97)+" = "+lowerCase[i]);
}
System.out.println();
System.out.println("Upper Case characters");
for(int i =0; i<upperCase.length; i++){
System.out.println("The frequency of the letter "+(char)(i+65)+" = "+upperCase[i]);
}
scan.close();
}
private static int charCount(char ch, String input) {
// TODO Auto-generated method stub
int chCount = 0;
for(int i =0; i<input.length(); i++){
if(input.charAt(i)== ch){
chCount++;
}
}
return chCount;
}
}
Output:
Enter input String: AbCdEfGhIjKlMnOpQrStUvWxYz
Lower Case characters
The frequency of the letter a = 0
The frequency of the letter b = 1
The frequency of the letter c = 0
The frequency of the letter d = 1
The frequency of the letter e = 0
The frequency of the letter f = 1
The frequency of the letter g = 0
The frequency of the letter h = 1
The frequency of the letter i = 0
The frequency of the letter j = 1
The frequency of the letter k = 0
The frequency of the letter l = 1
The frequency of the letter m = 0
The frequency of the letter n = 1
The frequency of the letter o = 0
The frequency of the letter p = 1
The frequency of the letter q = 0
The frequency of the letter r = 1
The frequency of the letter s = 0
The frequency of the letter t = 1
The frequency of the letter u = 0
The frequency of the letter v = 1
The frequency of the letter w = 0
The frequency of the letter x = 1
The frequency of the letter y = 0
The frequency of the letter z = 1
Upper Case characters
The frequency of the letter A = 1
The frequency of the letter B = 0
The frequency of the letter C = 1
The frequency of the letter D = 0
The frequency of the letter E = 1
The frequency of the letter F = 0
The frequency of the letter G = 1
The frequency of the letter H = 0
The frequency of the letter I = 1
The frequency of the letter J = 0
The frequency of the letter K = 1
The frequency of the letter L = 0
The frequency of the letter M = 1
The frequency of the letter N = 0
The frequency of the letter O = 1
The frequency of the letter P = 0
The frequency of the letter Q = 1
The frequency of the letter R = 0
The frequency of the letter S = 1
The frequency of the letter T = 0
The frequency of the letter U = 1
The frequency of the letter V = 0
The frequency of the letter W = 1
The frequency of the letter X = 0
The frequency of the letter Y = 1
The frequency of the letter Z = 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.