Write a program that reads multiple lines of text from the user, until the user
ID: 3710218 • Letter: W
Question
Write a program that reads multiple lines of text from the user, until the user enters the single word "quit". The program should keep track of the total number of appearances of each letter from 'a' to 'z' (lowercase and uppercase combined) in the line(s) of text entered by the user. When the user quits, it should display a table with the frequencies.
Hint 1: Use an ArrayList of 26 Integer objects to keep track of the number of appearances of each letter. In the beginning of your main method, create a new list and add to it the element 0, 26 times. From that point on, use the element at position 0 to keep track of the frequency of 'a', the element at position 1 to keep track of the frequency of 'b', and so on.
Hint 2: If c is a lowercase letter, the index of its frequency counter in the list is c - 'a'. (why??)
Hint 3: If c is an uppercase letter (between 'A' and 'Z'), then it can be transformed to lowercase with the expression (char) ('a' + c - 'A'). (why??)
Explanation / Answer
As it was mentioned array list i have implemented the code in JAVA
JAVA program
import java.util.ArrayList;
import java.util.Scanner;
public class WordFreq {
public static void main(String[] args) {
// Creating array List.
ArrayList<Integer> wordCount = new ArrayList<>();
// Initializing array list with 26 elements each as 0.
for (int i = 0;i <26; i++) {
wordCount.add(0);
}
// Acception input.
Scanner input = new Scanner(System.in);
// Accepting the first word
String in = input.next();
// If the word is quit then exit from the loop.
while(!in.equals("quit")) {
// Iterate over the word.
for(char c: in.toCharArray()) {
// Change the character to lower case.
// I have not checked for upper case or lower case for more easier logic.
char ch = Character.toLowerCase(c);
try {
// increment the character count by 1.
wordCount.set(ch-'a', wordCount.get(ch-'a') + 1);
}catch(ArrayIndexOutOfBoundsException e) {
// Ignoring other characters and punctuation marks.
}
}
// get the next word.
in = input.next();
}
// Print out the word frequencies
for(int i = 0; i < wordCount.size(); i++) {
char ch = (char) (i+'a');
System.out.println("Character "+ ch + " Count is " + wordCount.get(i));
}
}
}
Sample output
Hi there!!
Chegg is a good platform for all of us.
Have a great life.
thanks
quit
Character a Count is 7
Character b Count is 0
Character c Count is 1
Character d Count is 1
Character e Count is 6
Character f Count is 4
Character g Count is 4
Character h Count is 5
Character i Count is 3
Character j Count is 0
Character k Count is 1
Character l Count is 4
Character m Count is 1
Character n Count is 1
Character o Count is 5
Character p Count is 1
Character q Count is 0
Character r Count is 4
Character s Count is 3
Character t Count is 4
Character u Count is 1
Character v Count is 1
Character w Count is 0
Character x Count is 0
Character y Count is 0
Character z Count is 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.