Can anyone please help me write this C program Thank you The
Can anyone please help me write this C++ program? Thank you!
The main goal of this project is to complete a relatively straightforward C++ program by taking advantage of basic 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:
1.) 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 \"cout <<\" statement(s) to print out the frequencies on the screen.
2.) Implement the function void StrToTokens(string s); to identify and print all the tokens contained in s on the standard output (i.e., cout). For simplicity, only white spaces will be considered as delimiters. For instance, the string \"what\'s that thing?\" contains three tokens: \"what\'s\", \"that\" and \"thing?\".
You are not allowed to call existing functions such as strtok() for this task. Specifically, you are required to loop through the input string one character at a time to separate the input string to different tokens. Please store the tokens in an array (such as vector<string>) before printing out all the tokens.
3.) Implement the main() function that:
(1) declares and initializes the string s, and
(2) calls the above two functions.
Example input and output:
Suppose s=\"Always remember that you are unique. Just like everyone else.\"
The function getLetterFreq(s) will print out the following information (yours might be in a different format):
{\'a\': 4, \'b\': 1, \'e\': 11, \'i\': 2, \'h\': 1, \'k\': 1, \'j\': 1, \'m\': 2, \'l\': 3, \'o\': 2, \'n\': 2, \'q\': 1, \'s\': 3, \'r\': 4, \'u\': 4, \'t\': 3, \'w\': 1, \'v\': 1, \'y\': 3}
The output of StrToTokens(s); will be \'Always\', \'remember\', \'that\', \'you\', \'are\', \'unique.\', \'Just\', \'like\', \'everyone\', \'else.\'
Solution
// Example program
#include <iostream>
#include <string>
using namespace std;
void StrToTokens(string );
void getLetterFreq(string);
int main()
{
string name;
cout << \"What is your name? \";
getline (cin, name);
cout << name << \"\ \";
if(name.length() > 100){
cout << \" String holds more than 100 tokens. kindly enter the valid string having not more than tokens. \" << endl;
}
getLetterFreq(name);
StrToTokens(name);
}
void getLetterFreq(string input){
int frquency[26];
char alphabats[26] = { \'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\'};
for(int i = 0 ; i < 26 ; i++){
int count = 0;
for(int j = 0 ; j < input.length(); j++){
if(input[j] == alphabats[i]){
count++;
}
}
frquency[i] = count;
}
for(int k = 0 ; k < 26 ; k++ ){
cout << \"Letter \" << alphabats[k] << \" has frequency : \" << frquency[k] << endl ;
}
}
void StrToTokens(string input){
int startindx = 0 ;
int reIntilizePointer = 0 ;
int j= 0;
do{
char strchar[100];
for(int i = startindx ; i < input.length() ; i++)
{
if(input[i] != \' \' ){
startindx++;
strchar[j] = input[i];
j++;
}else{
startindx = startindx + 1 ;
reIntilizePointer = j;
j = 0;
break;
}
}
cout << strchar << endl ;
// just re-intilizing the array so that it doesn\'t hold the previous array values.
for( int k = 0 ; k < reIntilizePointer; k++ ){
strchar[k] = \' \';
}
}while(startindx < input.length());
}
There are two methods in above program except main method as per the solution needed. one is getLetterFreq which is responsible for priting letter and their frequency with respect to input given to program and another method is StrToTokens which isn responsible for displaying tokens avaibale in given input.
Thanks

