Java Programming: Write a complete program using two arrays, upper and lower to
ID: 3689311 • Letter: J
Question
Java Programming: 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 test from Jupiter. Soon you will see who is from Jupiter!!! May be Dr. D. 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:0
Explanation / Answer
//hey heres the code :
import java.util.Scanner;
class readstring
{
public static void main(String args[]){
int[] count=new int[26];
int[] count1=new int[26];
Scanner scanner = new Scanner(System.in);
String lower = "abcdefghijklmnopqrstuvwxyz";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String str;
//This is needed to pick up the new line
System.out.println("Enter some line:");
str=scanner.nextLine();
System.out.println(str);
for(int i=0;i<str.length();i++)
{
int flag=0;
if (Character.isLetter(str.charAt(i)))
{
for(int j=0;j<upper.length();j++)
{
if(str.charAt(i)==upper.charAt(j))
{
flag=1;
int k=(int)str.charAt(i)-65;
count[k]++;
break;
}
}
if(flag!=1)
{
for(int j=0;j<lower.length();j++)
{
if(str.charAt(i)==lower.charAt(j))
{
int k=(int)str.charAt(i)-97;
count1[k]++;
break;
}
}
}
}
}
for(int l=0;l<count.length;l++)
{
System.out.print(lower.charAt(l)+": "+count1[l]+" ");
System.out.print(upper.charAt(l)+": "+count[l]+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.