How do you wrie a profram in java that counts the frequency with which letters a
ID: 653331 • Letter: H
Question
How do you wrie a profram in java that counts the frequency with which letters appear in a string of text. The program should prompt the user for a line of text, then return the frequency of each letter in the text entered by the user. Treat upper and lowercase letters the same, non-letters are ignored. The program must use an array to hold the frequency count for each letter.
Sample run:
Please enter a string: Hello world! Happy day!
A 2
B 0
C 0
D 1
E 1
F 0
G 0
H 2
I 0
J 0
K 0
L 3
M 0
N 0
O 2
P 2
Q 0
R 1
S 0
T 0
U 0
V 0
W 1
X 0
Y 2
Z 0
Explanation / Answer
import java.util.Scanner;
public class AFC {
static int i,j,k,c=0,w;
static char m;
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter The String: ");
String in = scan.nextLine();
String newstr = in.replaceAll("[^A-Za-z]+", "");
String strng = newstr.toUpperCase();
frequencycount(strng);
}
static void frequencycount(String s)
{
char[] z=new char[s.length()];
for(w=0;w<s.length();w++)
z[w]=s.charAt(w);
for(i=0;i<w;i++)
{
char ch=z[i];
for(j=i+1;j<w;j++)
{
if(z[j]==ch)
{
for(k=j;k<(w-1);k++)
z[k]=z[k+1];
w--;
j=i;
}
}
}
int[] t=new int[w];
for(i=0;i<w;i++)
{
for(j=0,c=0;j<s.length();j++)
{
if(z[i]==s.charAt(j))
c++;
}
t[i]=c ;
System.out.print(z[i]+" "+c+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.