This will be done in C++ The main goal of this first homework is to complete a r
ID: 3790879 • Letter: T
Question
This will be done in C++
The main goal of this first homework is to complete a relatively straightforward C++ program by taking advantage of your Java knowledge. Keep in mind that at the statement level, if your code can compile and run in Java, it will pretty much be accepted by C++ and also does what you expect in C++. Problem description: Given a string variable string s: initialize s such that it contains a paragraph in English text. You can do so within your program by either (1) hardcoding the initial value, e.g, string s("hello world"): or (2) read in the initial value from the keyboard by calling the getline() function. Furthermore, this paragraph consists of no more than 100 tokens. Tokens are sequences of contiguous characters separated by any of the specified delimiters (e.g., white spaces). Please implement a C++ program to perform the following two tasks on s: Implement the function void getLetterFreq(string s): to identify the frequency of each unique letter ('a' - 'z', case insensitive) in s. This function will call the "coutExplanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
using namespace std;
void getLetterFreq(string s)
{
char ch = 'a';
int c;
cout << " Character frequency ";
while(ch != 'z')
{
c = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == ch)
{
++ c;
}
}
if(c > 0)
{
cout << ch << ": " << c << endl;
}
ch++;
}
}
void StrToTokens(string s)
{
cout << "Word tokens ";
string str = "";
for (int i = 0; i < s.size(); ++i)
{
// print the word if space is found
// and instantiate new word
if(s[i] == ' ')
{
cout << str << endl;
str = "";
}
// else add to the corrent word
else
{
str = str + s[i];
}
}
cout << endl;
}
int main()
{
string s = "Always remember that you are unique. Just like everyone else.";
StrToTokens(s);
// string to lower case
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
getLetterFreq(s);
return 0;
}
/*
output:
Word tokens
Always
remember
that
you
are
unique.
Just
like
everyone
Character frequency
a: 4
b: 1
e: 11
h: 1
i: 2
j: 1
k: 1
l: 3
m: 2
n: 2
o: 2
q: 1
r: 4
s: 3
t: 3
u: 4
v: 1
w: 1
y: 3
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.