Question: Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs.

Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both A and a should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once in the text, print out the number of times it appeared (and do so in alphabetical order). An effective way to count characters is to read from the console string-by-string, and loop through all of the characters of each of these strings. Similar to Problem a, the hasNext() method of the scanner will be useful, and when testing in Eclipse, press enter and then, once on the empty line, press CTRL-D when you are done typing the text. You must use an array to keep track of how many times each letter is seen in the text. The array should have 26 elements (one for each letter in the alphabet). Index 0 should be used to track the number of As in the file, index 1 to track the Bs, index 2 to track the Cs, etc., up to index 25 for the Zs. You could use a massive if/else block, but the whole reason to use arrays is to make your programs easier. So, instead, think about how to convert each character you read into the correct index and then increment that value in the array. For example, if you read an A, then you should increment the value in index 0. Specifically, you will need to determine if the character is an uppercase letter (between A and Z), a lowercase letter (between a and z), or something else. If it is a letter, convert it into the appropriate index. Recall that characters and integers are interchangeable via the ASCII table conversion1. Consider this example to help get you started: char input = 'z'; int index = input - 'a'; // index equals 25, as 'z' is 122 and 'a' is 97 Using this code: public static void main(String[] args) { //two arrays to count capital and small letters count int[] capital=new int[26]; int[] small=new int[26]; Scanner scanner=new Scanner(System.in); String word; System.out.println("Enter text: "); //read until user enter button and cnlt+z while(scanner.hasNext()) { //read word by word word=scanner.next(); for (int i = 0; i < word.length(); i++) { char ch=word.charAt(i); if(isCapital(ch)) { int index=getCapitalIndex(ch); //increment the index postion of the capital letter //array capital[index]++; } else if(isSmall(ch)) { int index=getSmallIndex(ch); //increment the index postion of the small letter //array small[index]++; } } } //print letter and count of small letters System.out.println("Small Letters count"); char letter='a'; for (int i = 0; i < small.length; i++,letter++) { System.out.printf("%-4c= %-5d ",letter,small[i]); } letter='A'; System.out.println("Capital Letters count"); //print letter and count of capital letters for (int i = 0; i < small.length; i++,letter++) { System.out.printf("%-4c= %-5d ",letter,capital[i]); } }//end of main method //Return index position of small letter private static int getSmallIndex(char ch) { return ch-'a'; } //Return index position of capital letter private static int getCapitalIndex(char ch) { return ch-'A'; } //Returns true if ch is capital private static boolean isCapital(char ch) { if(ch>='A' && ch<='Z') return true; else return false; } //Returns true if ch is small private static boolean isSmall(char ch) { if(ch>='a' && ch<='z') return true; else return false; } }//end of class I JUST NEED PRINTED ONLY ( O N L Y) THE LETTERS THAT HAVE BEEN PRINTED. For example, if a has 1 then a: 1, if b does't have any then DO NOT print b in the printer!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!