Question

Write a program that reads a string from the keyboard and computes the two arrays of...

Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format:

The frequency of the letter A=

The frequency of the letter B=

Assume the input string contains only uppercase and lower case letters. Your solution should be efficient and it does not use a sequence of 26 IF statements or a Switch statement.

You may need the casting operation to convert int to char and vice versa.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code for the given requirement:

import java.util.Scanner;

public class Assignment {

      public static void main(String [] args){

            //Getting the input String from user

            Scanner scan = new Scanner(System.in);

            System.out.print("Enter input String: ");

            String input = scan.nextLine();

            //creating two arrays

            int lowerCase[]=new int[26];

          int upperCase[]=new int[26];

          //using for loops for counting each character

            for(char ch = 'a'; ch<='z'; ch++){

                  lowerCase[(int)ch-97] = charCount(ch,input) ;

            }

            for(char ch = 'A'; ch<='Z'; ch++){

                  upperCase[(int)ch-65] = charCount(ch,input) ;

            }

            System.out.println("Lower Case characters");

            for(int i =0; i<lowerCase.length; i++){

                  System.out.println("The frequency of the letter "+(char) (i+97)+" = "+lowerCase[i]);

            }

            System.out.println();

            System.out.println("Upper Case characters");

            for(int i =0; i<upperCase.length; i++){

                  System.out.println("The frequency of the letter "+(char)(i+65)+" = "+upperCase[i]);

            }

            scan.close();

      }

      private static int charCount(char ch, String input) {

            // TODO Auto-generated method stub

            int chCount = 0;

            for(int i =0; i<input.length(); i++){

                  if(input.charAt(i)== ch){

                        chCount++;

                  }

            }

            return chCount;

      }

}

Output:

Enter input String: AbCdEfGhIjKlMnOpQrStUvWxYz

Lower Case characters

The frequency of the letter a = 0

The frequency of the letter b = 1

The frequency of the letter c = 0

The frequency of the letter d = 1

The frequency of the letter e = 0

The frequency of the letter f = 1

The frequency of the letter g = 0

The frequency of the letter h = 1

The frequency of the letter i = 0

The frequency of the letter j = 1

The frequency of the letter k = 0

The frequency of the letter l = 1

The frequency of the letter m = 0

The frequency of the letter n = 1

The frequency of the letter o = 0

The frequency of the letter p = 1

The frequency of the letter q = 0

The frequency of the letter r = 1

The frequency of the letter s = 0

The frequency of the letter t = 1

The frequency of the letter u = 0

The frequency of the letter v = 1

The frequency of the letter w = 0

The frequency of the letter x = 1

The frequency of the letter y = 0

The frequency of the letter z = 1

Upper Case characters

The frequency of the letter A = 1

The frequency of the letter B = 0

The frequency of the letter C = 1

The frequency of the letter D = 0

The frequency of the letter E = 1

The frequency of the letter F = 0

The frequency of the letter G = 1

The frequency of the letter H = 0

The frequency of the letter I = 1

The frequency of the letter J = 0

The frequency of the letter K = 1

The frequency of the letter L = 0

The frequency of the letter M = 1

The frequency of the letter N = 0

The frequency of the letter O = 1

The frequency of the letter P = 0

The frequency of the letter Q = 1

The frequency of the letter R = 0

The frequency of the letter S = 1

The frequency of the letter T = 0

The frequency of the letter U = 1

The frequency of the letter V = 0

The frequency of the letter W = 1

The frequency of the letter X = 0

The frequency of the letter Y = 1

The frequency of the letter Z = 0

Add a comment
Know the answer?
Add Answer to:
Write a program that reads a string from the keyboard and computes the two arrays of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • u also need to store the letters' ASCII number in the freq array 4. [8] Write...

    u also need to store the letters' ASCII number in the freq array 4. [8] Write a C function with prototype · void letter_freq(const char word[], int freq []); This function computes the number of appearances of each letter in the string word and stores them in array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90...

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • 1. Write a python program that reads a file and prints the letters in increasing order...

    1. Write a python program that reads a file and prints the letters in increasing order of frequency. Your program should convert entire input to lower case and only counts letters a-z. Special characters or spaces should not be counted. Each letter and it's occurrences should be listed on a separate line. 2. Test and verify your program and it's output. ---Please provide a code and a screenshot of the code and output. Thank you. ----

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Please do it with C++. Thanks:) Write a program that reads a string and a character...

    Please do it with C++. Thanks:) Write a program that reads a string and a character from the keyboard. The program should output how many times the character appears in the string. Make the program run in a loop until the string finish is input. Sample run: Input a string: alicia Input a letter: a The letter appears 2 times Input a string: felix Input a letter: x The letter appears 1 time Input a string: finish Good Bye.

  • General Requirements . . . Write a program that reads letters from a file called "letters.txt"....

    General Requirements . . . Write a program that reads letters from a file called "letters.txt". Your program will open the letters.txt file read in one character at a time For this assignment the test file will contain at least 30 letters, uppercase OR lowercase In your program you will change each letter (solution) to uppercase Use the function toupper in #include <ctype.h> You create a numerical version of the uppercase letter from the file . o Use int numberSolution...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT