Please help me with writing this program. I need some help on how to approach th
ID: 3542465 • Letter: P
Question
Please help me with writing this program. I need some help on how to approach this problem and what functions I would need. Thank you in advance.
Write a program to read some arbitrary text from the user and display a histogram of various characters in the input text. A histogram computes the number of occurrences of each character in the input. For example, for an input of "Hello World. All the world is a stage.", the output should be: H: 1, e: 3,I: 6, o: 3, W: 1, r: 2, d: 2, :.2, A: 1, t: 2, h:l, w:l, i:l, s:2, a:2, g:1. Assume that the input only contains alphabets and periods and ignore spaces in the input. Your program should be modular - so declare and implement functions to solve your problem. For example, you should have a function to read the text, a function to compute the histogram and another function to display the output. The function to compute the histogram should use additional functions for repetitive tasks if any.Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
string read_text()
{
string local;
cout <<"Enter line of text to read ";
getline(cin,local);
return local;
}
void compute_histogram(int a[256],string text)
{
for(int i=0; i<text.length(); i++)
{
if(text[i]>=65 && text[91]<=91)
a[text[i]]++;
else if(text[i]>=97 && text[91]<=122)
a[text[i]]++;
else if(text[i]=='.')
a[text[i]]++;
}
}
void display_output(int a[256],string text)
{
bool saw[256] = {false};
cout << endl;
for(int i=0; i<text.length(); i++)
{
if(a[text[i]]!=0 && !saw[text[i]])
{
cout << text[i] << " : " << a[text[i]] << endl;
saw[text[i]] = true;
}
}
}
int main()
{
string input;
input = read_text();
int a[256] = {0};
compute_histogram(a,input);
display_output(a,input);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.