*Note* This is a beginner introduction course to Java & a this is a java Program
ID: 3844521 • Letter: #
Question
*Note* This is a beginner introduction course to Java & a this is a java Program*
Write a complete program using two arrays, upper and lower to keep the upper
and lower alphabet respectively.
Ask the user to enter string example: This is a hard test! bye.
Your program should parse the string and keep track of number of alphabet. Both arrays are indexed from 0 to 25. The logical way to do this is to use upper[0] to
Count the number of ‘A’, and upper[1] to count number of ‘B’ and so on. Likewise
for the lower array.
Output should look like:
A: 0 a:2
B: 0 b:1
….
Please do not use any advanced programing! This is an intro course.
Explanation / Answer
package org.students;
import java.util.Scanner;
public class UpperLowerArray {
public static void main(String[] args) {
// Declaring variables
int len;
String str;
// Creating two integer type arrays
int Upper[] = new int[26];
int Lower[] = new int[26];
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the string entered by the user
System.out.print("Enter the String :");
str = sc.nextLine();
/*
* count upper case and lower case characters in the string and
* increment the count in the arrays
*/
for (int i = 0; i < str.length(); i++) {
if ((int) (str.charAt(i)) >= 65 && (int) (str.charAt(i)) <= 90) {
Upper[((int) (str.charAt(i))) - (65)]++;
} else if ((int) (str.charAt(i)) >= 97
&& (int) (str.charAt(i)) <= 122) {
Lower[(int) (str.charAt(i)) - 97]++;
}
}
// Displaying the upper and lower character frequencies
for (int i = 0; i < 26; i++) {
System.out.printf("%c:%d %c:%d ", (65 + i), Upper[i], (97 + i),Lower[i]);
}
}
}
_____________________
Output:
Enter the String :This is a hard test! bye.
A:0 a:2
B:0 b:1
C:0 c:0
D:0 d:1
E:0 e:2
F:0 f:0
G:0 g:0
H:0 h:2
I:0 i:2
J:0 j:0
K:0 k:0
L:0 l:0
M:0 m:0
N:0 n:0
O:0 o:0
P:0 p:0
Q:0 q:0
R:0 r:1
S:0 s:3
T:1 t:2
U:0 u:0
V:0 v:0
W:0 w:0
X:0 x:0
Y:0 y:1
Z:0 z:0
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.