1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
anygoal [31]
4 years ago
5

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 m's, 3 o's, 2 e's, 2 y's and one each of d, g, a, t, h, w, r and k. 2 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. [Hint: 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 o 3 r 1 t 1 w 1 y 2

Computers and Technology
1 answer:
Nostrana [21]4 years ago
6 0

Answer:

Check the explanation

Explanation:

Code to copy:

// ConsoleApplication7.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

int * letterFrequency(char s[]);

int main()

{

  int *freq_letters;

  char *s = new char[100];

  freq_letters = new int[26];

  //To read the input string in order to find the frequency of each letter

  cout << "Enter a string: ";

  cin.getline(s, ' ');

  //call the function to find the occurrence of each alphabet

  freq_letters = letterFrequency(s);

  //Display the count

  cout << "Letter Frequency " << endl;

  for (int i = 0; i < 26; i++)

  {

      //Constriant to check if the letter appeared at least once in the string and so printing the frequency of its occurence

      if (freq_letters[i] != 0)

      cout << " " << static_cast<char>(i + 'a') << " " << freq_letters[i] << endl;

  }

  system("pause");

  return 0;

}

//Define the function to find occurrence of each letter in the input string

int * letterFrequency(char s[])

{

  int *occurrence_array;

//to store the output of occurrences for each alphabet

  occurrence_array = new int[26];

// to store the count of occurrence for each letter temporarily

  int letter_count;

  // for loop to check the occurrence for all 26 alphabets

 

  for (int i = 0; i < 26; i++)

  {

      letter_count = 0;

      for (int j = 0; j < strlen(s); j++)

      {

          /*comparing the ascii values of each alphabet with every character from the string by converting it to lower case i.e. case insensitive*/

          if (int('a') + i == int(tolower(s[j])))

              letter_count++;

      }

      occurrence_array[i] = letter_count;//To store the count calculated for each alphabet

  }

  return occurrence_array;

}

The following below code screenshot and output shows that when the string is entered, the output will shows each lettercount irrespective of whether the letter is in capitals or small and does not count non-letter characters (i.e spaces, punctuations etc.)

You might be interested in
You can change the ____ or position of text within a document's margins.
ahrayia [7]

Answer:

Alignment

Explanation:

That should be correct

7 0
3 years ago
Certain country in the Caribbean Sea recently held an election to choose its president. Every vote cast was electronic, but unfo
lesya [120]

Answer:

Certain country in the Caribbean Sea recently held an election to choose its president. Every vote cast was electronic, but unfortunately, a recent power surge caused a malfunction in the system just before votes were counted. The only information saved consists of the following facts:

Explanation:

• All the N citizens casted their vote.

• Exactly one candidate received more than N/2 votes.

• We don’t know how many candidates there were.

You are hired to help using your expertise in algorithms.

5 0
2 years ago
What movie would be greatly improved if it was made into a musical?
Lina20 [59]
Sixteen candles would be even better if it was a musical
3 0
3 years ago
HELP is web designing a career in graphic design yes or no?
Ilya [14]

Answer:

Web Design, just like Graphic Design, is the creation of graphics, typography, graphs, and images to communicate an idea. However, Web Design only concerns websites, not print. ... They also have to take their designs and turn them into a working website, which involves programming.

Explanation:

In other words no

6 0
3 years ago
Sharon reads two different articles about avocados. The first article, in a weight loss magazine, claims that avocados are unhea
padilas [110]
The first and third.
3 0
3 years ago
Read 2 more answers
Other questions:
  • This formatting option functions like a space bar. However, instead of moving one space at a time, it allows you to move your te
    6·1 answer
  • Which of the following was the first commercial software package to incorporate WYSIWYG as a feature?
    15·1 answer
  • Which statement about the Paste Link option is true?
    6·1 answer
  • TOPIC-PYTHON
    8·2 answers
  • 15) When you buy an operating system for your personal computer, you are actually buying a software ________. A) copyright BE) u
    9·2 answers
  • What is the education level of a majority of the Power, Structural, and Technical Systems workers?
    7·1 answer
  • Determine the distance between point (x1, y1) and point (x2, y2), and assign the result to points Distance. The calculation is:
    12·1 answer
  • El planeamiento estratégico de una empresa es​
    9·1 answer
  • Complete the sentence.
    10·1 answer
  • Can someone help me explain Nvm scheduling in operating systems?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!