Write a Java program that reads a string from the user, and then prints out the
ID: 3758412 • Letter: W
Question
Write a Java program that reads a string from the user, and then prints out the count of each letter (regardless of case) as shown below. Note that you need to use separate counter for each letter, that is 26 counters, named as aCount, bCount, cCount, dCount, …. Print out only the counters that count the letters used in the string (that is, counters with values > 0 only). Do not printer counters with 0 values. Use proper labels for the outputs as shown in the examples below.
Entered String: This is a test input.
Letter A: 1
Letter E: 1
Letter H: 1
Letter I: 3
Letter N: 1
Letter P: 1
Letter S: 3
Letter T: 4
Letter U: 1
Explanation / Answer
import java.util.*;
class CharacterCount {
public static void main(String[] args) {
int acount=0,bcount=0,ccount=0,dcount=0,ecount=0,fcount=0;
int gcount=0,hcount=0,icount=0,jcount=0,kcount=0,lcount=0;
int mcount=0,ncount=0,ocount=0,pcount=0,qcount=0,rcount=0;
int scount=0,tcount=0,ucount=0,vcount=0,wcount=0,xcount=0;
int ycount=0,zcount=0;
Scanner in = new Scanner(System.in);
System.out.println("Entered String: ");
String st = in.nextLine();
char str[]=st.toCharArray();
int length=str.length;
for(int i=0;i<length;i++) {
if(str[i]=='a' || str[i]=='A')
acount++;
else if(str[i]=='b' || str[i]=='B')
bcount++;
else if(str[i]=='c' || str[i]=='C')
ccount++;
else if(str[i]=='d' || str[i]=='D')
dcount++;
else if(str[i]=='e' || str[i]=='E')
ecount++;
else if(str[i]=='f' || str[i]=='F')
fcount++;
else if(str[i]=='g' || str[i]=='G')
gcount++;
else if(str[i]=='h' || str[i]=='H')
hcount++;
else if(str[i]=='i' || str[i]=='I')
icount++;
else if(str[i]=='j' || str[i]=='J')
jcount++;
else if(str[i]=='k' || str[i]=='K')
kcount++;
else if(str[i]=='l' || str[i]=='L')
lcount++;
else if(str[i]=='m' || str[i]=='M')
mcount++;
else if(str[i]=='n' || str[i]=='N')
ncount++;
else if(str[i]=='o' || str[i]=='O')
ocount++;
else if(str[i]=='p' || str[i]=='P')
pcount++;
else if(str[i]=='q' || str[i]=='Q')
qcount++;
else if(str[i]=='r' || str[i]=='R')
rcount++;
else if(str[i]=='s' || str[i]=='S')
scount++;
else if(str[i]=='t' || str[i]=='T')
tcount++;
else if(str[i]=='u' || str[i]=='U')
ucount++;
else if(str[i]=='v' || str[i]=='V')
vcount++;
else if(str[i]=='w' || str[i]=='W')
wcount++;
else if(str[i]=='x' || str[i]=='X')
xcount++;
else if(str[i]=='y' || str[i]=='Y')
ycount++;
else if(str[i]=='z' || str[i]=='Z')
zcount++;
}
if (acount > 0 )
System.out.println("Letter A: "+acount);
if (bcount > 0 )
System.out.println("Letter B: "+bcount);
if (ccount > 0 )
System.out.println("Letter C: "+ccount);
if (dcount > 0 )
System.out.println("Letter D: "+dcount);
if (ecount > 0 )
System.out.println("Letter E: "+ecount);
if (fcount > 0 )
System.out.println("Letter F: "+fcount);
if (gcount > 0 )
System.out.println("Letter G: "+gcount);
if (hcount > 0 )
System.out.println("Letter H: "+hcount);
if (icount > 0 )
System.out.println("Letter I: "+icount);
if (jcount > 0 )
System.out.println("Letter J: "+jcount);
if (kcount > 0 )
System.out.println("Letter K: "+kcount);
if (lcount > 0 )
System.out.println("Letter L: "+lcount);
if (mcount > 0 )
System.out.println("Letter M: "+mcount);
if (ncount > 0 )
System.out.println("Letter N: "+ncount);
if (ocount > 0 )
System.out.println("Letter O: "+ocount);
if (pcount > 0 )
System.out.println("Letter P: "+pcount);
if (qcount > 0 )
System.out.println("Letter Q: "+qcount);
if (rcount > 0 )
System.out.println("Letter R: "+rcount);
if (scount > 0 )
System.out.println("Letter S: "+scount);
if (tcount > 0 )
System.out.println("Letter T: "+tcount);
if (ucount > 0 )
System.out.println("Letter U: "+ucount);
if (vcount > 0 )
System.out.println("Letter V: "+vcount);
if (wcount > 0 )
System.out.println("Letter W: "+wcount);
if (xcount > 0 )
System.out.println("Letter X: "+xcount);
if (ycount > 0 )
System.out.println("Letter Y: "+ycount);
if (zcount > 0 )
System.out.println("Letter Z: "+zcount);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.