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
What reason best explains why complementary colors are important to web page design?
Brums [2.3K]

Complementary colors are important to web page design because they will inspire the visitor. For example, complementary colors make your website's text easier to red. They contrast with one another, so that they create a harmony for your page.

4 0
3 years ago
Read 2 more answers
GoodArray hackerrank solution for a number N, a goodArray is the smallest possible array that consists of only powers of two
Paladinen [302]

Using the knowledge in computational language in python it is possible to write a code that smallest possible array that consists of only powers of two.

<h3>Writting the code in python:</h3>

<em>"public class GoodArray {"</em>

<em />

<em> "public static List<Integer> getQueryResults(long N, List<List<Integer>> queries) {"</em>

<em />

<em>  "List<Integer> res = new ArrayList<>();"</em>

<em>  int[][] arr = new int[queries.size()][3];</em>

<em>  "List<Integer> goodArray = new ArrayList<>();"</em>

<em>  "for (int i = 1; i <= N; i++) {"</em>

<em>   "int num = i;"</em>

<em>   "while (num % 2 == 0) {"</em>

<em>    "goodArray.add(num);"</em>

<em>    "num = num / 2;"</em>

<em>   }</em>

<em>  }</em>

<em>  int index = 0;</em>

<em>  for (List<Integer> l : queries) {</em>

<em>   arr[index][0] = l.get(0);</em>

<em>   arr[index][1] = l.get(1);</em>

<em>   arr[index][2] = l.get(2);</em>

<em>   index++;</em>

<em>  }</em>

<em>  Collections.sort(goodArray);</em>

<em>  "for (int i = 0; i < arr.length; i++) {"</em>

<em>   "int[] query = arr[i];"</em>

<em>   int l = query[0];</em>

<em>   int r = query[1];</em>

<em>   int m = query[2];</em>

<em>   int prod = 1;</em>

<em>   "for (int j = l - 1; j <= r - 1; j++) {"</em>

<em>    "prod = (int) (prod * goodArray.get(j)) % m;"</em>

<em>   }</em>

<em>   res.add(prod);</em>

<em>  }</em>

<em>  return res;</em>

<em> }</em>

<em />

<em>}</em>

See more about python at brainly.com/question/18502436

#SPJ1

8 0
2 years ago
Why you chose BSIT?​
chubhunter [2.5K]

Answer:

Because of choice.

Explanation:

meaningful Question py

6 0
3 years ago
Read 2 more answers
Is this photo considered rim photography?
steposvetlana [31]

wheres the picture??

8 0
3 years ago
Assume that you want to send your Mom a picture file, continuously as one large message, over a link that has a rate of 1Mbps. Y
Mandarinka [93]

Answer: you should listen in your classroom

Explanation: of you listened you would not be asking this qustion now would you

4 0
3 years ago
Other questions:
  • Members of which generation were born after the turn of the millennium?
    11·1 answer
  • A touch screen is classified as a special type of scanning device true or false
    15·1 answer
  • Different between ocular and compound miscroscope
    13·1 answer
  • Bitlocker uses the computer's __________ chip to store encryption keys and does not rely on individual user credentials. securit
    10·1 answer
  • I thought the answer was senior manager see on google it says
    8·1 answer
  • "You are working on a Debian distribution of Linux. You need to install a package, but you do not want to manually install all t
    8·1 answer
  • 15. The most efficient way to perform data entry is to keep your hands on the keyboard and press _______ to move to the next cel
    13·1 answer
  • Can some one help me i do not now how to give a BRANLEST. if you help i will give you one BRANLEST.
    7·2 answers
  • Which output device would a teacher use to show
    13·1 answer
  • What are interpersonal skills for non-technical user
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!