Question

C++ Linux Compiler

2) Letter Frequency Write a function that will take a string and return a count of each letter in the string. For example, my dog ate my homework contains 3 ms, 3 os, 2 es, 2 ys and one each of d, g, a, t, h, w, r and k. Your function should take a single string argument and return a dynamically allocated array of 26 integers representing the count of each of the letters a z respectively. Your function should be case insensitive, i.e count A and a as the occurrence of the letter a. IHint: use the letter to integer conversion functions.] Do not count non-letter characters (i.e., spaces, punctuation, digits, etc.) Write a program that will solicit a string from the user using getline, call your letter frequency function and print out the frequency of each letter in the string. Do not list letters that do not occur at least once. Example: Enter a string: my dog at my homework Letter frequency a 1 d 1 e 1 g 1 h 1 k 1 m 3 r 1 t 1 W 1 y 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Source Code:

import java.io.*;

class Frequency
{
static String n;
static int l;
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter a String : ");
 n = br.readLine();
 l = n.length();
 freq();
 }
 public static void freq()
 {
 int s=0,f=-1;
 for(int i=0;i<l;i++)
 {
  // Find frequecy
  for(int j=0;j<l;j++)
  {
   if(n.charAt(i)==n.charAt(j))
   s++;
  }

  // Check if the letter has occured before
  for(int k=0;k<i;k++)
  {
   if(n.charAt(i)==n.charAt(k))
   f = 1;
  }

  // Print the letter's frequency
  if(f==-1)
   System.out.println(n.charAt(i) +" = " +s);
  s=0;
  f=-1;
 }
 }
}
  • Output:
  • Enter a String : reverberate
    r = 3
    e = 4
    v = 1
    b = 1
    a = 1
    t = 1
  • Preview:
  • File Edit Search View Encoding Language Settings Macro Run Plugins Window? mport Java.io. 2 class Frequency (static String n
  • Output:
  • erEenith Desktopjavac Frequency.java CNUserrsEenith Desktop>java Frequency nter a StringWelconcThis is java progranning Users
Add a comment
Know the answer?
Add Answer to:
C++ Linux Compiler Letter Frequency Write a function that will take a string and return a...
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
  • Please write a function in C++ named nextString that will return a single 'value' (i.e. substring)...

    Please write a function in C++ named nextString that will return a single 'value' (i.e. substring) from a Comma Separated Value" string. Your function will take two arguments: a string variable containing a comma separated list of values, and an integer containing the starting index; your function should return a single string object with the value that starts at that index and ends right before the next comma ',' (do not include the comma in the returned string!) : string...

  • Python 3: Write a function CharCount, to take a string S and a single character C,...

    Python 3: Write a function CharCount, to take a string S and a single character C, and returns the count of C (one letter) in the string S. If the letter of C is not found in S, the function returns -1. You must write your own function to do this and can not use any built-in counting functions. Use the function in main to get two inputs from the user, a sentence and a character, then show the count...

  • Write an LC-3 program (starting at memory location 0x3000) to take a string as input and...

    Write an LC-3 program (starting at memory location 0x3000) to take a string as input and then output information about this string. The end of the string will be denoted with the "#" character. Once the "#" has been found, output the following in order: 1) The letter “u” followed by the number of uppercase letters in the string (A-Z) 2) The letter “l” followed by the number of lowercase letters in the string (a-z) 3) The letter “n” followed...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • For this problem, you will write a program to form a frequency table of the letters...

    For this problem, you will write a program to form a frequency table of the letters in a text file, that is, how many times each letter appears in the file In Python 3.7. Such a frequency table might be useful for compressing a text file. Because different letters appear with different frequencies, we can compress a file by using shorter codes for common letters and longer codes for letters that appear less frequently. Dictionaries provide an elegant way to...

  • 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...

  • Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count...

    Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count the number of times each letter appears in a string #using a dictionary. #letterAppearance returns only the letters that appear in the string. #Capitalization should not matter ''' letterAppearance('hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('Hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('mutation') #{'m': 1, 'u': 1, 't': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1} '''

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • PYTHON The first function you will write should be called ‘countDown’. Your function should take zero...

    PYTHON The first function you will write should be called ‘countDown’. Your function should take zero (0) arguments. The function should return one (1) list containing integers from 10 to 1 and finally, the string “Liftoff!”. (i.e., [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ‘Liftoff!’]). You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc). 3. Function...

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