Question

In: Computer Science

Write a program that will read a line of text. Display all the letters that occure...

Write a program that will read a line of text. Display all the letters that occure in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26, so that the element at index 0 contains the number of a’s, the element at index 1 contains the number of b’s, and so forth, Alloow both upperCase and lower Case. Define a method that takes a character as an argument and returns an int value that is the correct index for that character. For example, the argument ‘a’ results in 0 as the return value, the argument ‘b’ gives 1 as the return value, and so on. Allow the user to repaet this task until the user says she/he is through.

JAVA

Solutions

Expert Solution

Please find your solution below and if any doubt do comment and do upvote.

import java.util.*;
public class Main
{
    //return the index for respective Character
    public static int getIndex(char ch){
        return (int)(ch-'a');
    }
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                while(true){
                    //array to store the frequence
                    int []charFrequency=new int[26];
                    //take a line of text from user
                    String str=sc.nextLine();
                    //convert it to lower case since 
                    //upper and lower are to be treated same 
                    //for this program
                    str=str.toLowerCase();
                    //condition to break out from the loop
                    if(str.equals("she is through")||str.equals("he is through")){
                        break;
                    }
                    for(int i=0;i<str.length();i++){
                        //determines if it alphabet or not
                        if(Character.isLetter(str.charAt(i))){
                            charFrequency[getIndex(str.charAt(i))]+=1;
                        }
                    }
                    //display the frequence of Character
                    for(int i=0;i<26;i++){
                        if(charFrequency[i]==0){
                            continue;
                        }
                        else{
                            System.out.println("The letter "+(char)(i+'A')+" occured "+charFrequency[i]+" times in the text");
                        }
                    }
                }
        }
}


Related Solutions

Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Write a Java program to read in text line by line until a blank line is...
Write a Java program to read in text line by line until a blank line is entered. When this occurs the program returns the total number of words and characters found as shown in the example below. A program to count the number of words and characters in an input text block. Enter text line by line; blank line to end. >> This is the first line of text >> Followed by the second line of text >> Ending with...
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based...
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based on the HD44780 controller. Apply the following: The LCD instruction/data bus is connected to PORTD, and control bus is connected to PORTB (RS is connected to PB0, R/W to PB1, E to PB2). Use the #define command to name PORTD as Display_port. Use the #define command to name PORTB as Control_port. The displayed information is stored in the microcontroller Flash ROM (maximum size of...
Write a program that counts the letters in a given string and then display the letter...
Write a program that counts the letters in a given string and then display the letter count in order from high to low. The program should: Display a message stating its goal Prompt the user to enter a string input Count the letters in the string (hint: use dictionaries) Display the letter count in order from high to low (sort your letter count) For example, for the input Google the output should be G - 2 O - 2 E...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
In C++, write a program that will read up to 10 letters (characters) into an array...
In C++, write a program that will read up to 10 letters (characters) into an array and write the letters back to the screen in the reverse order. The program should prompt the user to input all values and can determine whether the input has ended by a punctuation mark such as ‘.’ (make sure that you’ll include meaningful and intuitive messages to the user). For example, if the input is abcde, the output should be edcba. Include appropriate messaging...
in java (just a line of code will do) Your program will read in the text...
in java (just a line of code will do) Your program will read in the text file, word by word. You are to ignore leading and trailing punctuation and capitalization (i.e., "Castle", "castle?", and "CASTLE" are all equivalent to castle). Convert all words to lower case on input, or you can use case-insensitive comparisons - your choice. You will be putting the words into several implementations of linked lists and comparing the results. If, after trimming punctuation, a "word" consists...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a PHP Program to display all the $_SERVER elements
Write a PHP Program to display all the $_SERVER elements
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT