Need help writing the following code. Language is C++, I\'m using vim. Thanks (6
ID: 3605785 • Letter: N
Question
Need help writing the following code. Language is C++, I'm using vim. Thanks
(6 pts total) Design First (3 pts), Then Write Code (3 pts)!!! To help you practice functions for Assignment #4, you will write a short program that asks the user to enter a string (get_string(), makes a copy of the string, takes the copy and changes all non-space letters to dashes (set_replace_string)), and gets a letter from the user to search and replace returning the number of letters found (get _search_replace(0). You should design first... You can have more functions, but you must have at least the three below with the EXACT function prototypes Each of your functions should have the proper function headers/descriptions void get_string(string); void set_replace_string(string, string *) int get_search_replace(string, string &) Write the function headers/descriptions for each of the functions above, as well as all the functions you create! This includes information about parameters, return values, and pre/post conditions. Design - Give as much detail as possible for the main function and all the functions above. o Why do the function prototypes have the specific parameter types on them? o Why do the functions have void or a return value? o How do all the functions interact together? Testing Provide testing values with expected results. What do you plan to use as bad values? Good values? What do you expect to happen with bad values? Good values? o oExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
//fnction declarations
void get_string(string *);
void set_replace_string(string s, string *ptr);
int get_search_replace(string s, string &);
int main()
{
string s, str, s1, replacedStr;
get_string(&s);
cout << "Entered string is : " << s<<endl;
set_replace_string(s, &str);
cout << "Replaced string is : " << str << endl;
cout << "Enter letter to serach followed by letter to replace: ";
cin >> s1;
int ret = get_search_replace(s1, s);
cout << endl<<"letter "<<s1[0]<<" found "<<ret<<" times in this string ."<<"Serach letter "<<s1[1]<<" and after search and replace string is : " << s << endl;
}
void get_string(string *str)
{
getline(cin, *str);
}
void set_replace_string(string s, string *ptr)
{
for (int i = 0; i < s.length(); i++)
{
//if letter is not space replace with dash '-' ,otherwise leave as it is
if (s[i] != 32)
(*ptr).push_back('-');
else
(*ptr).push_back(s[i]);
}
}
int get_search_replace(string s, string &str)
{
int count = 0;
for (int j = 0; j < str.length(); j++)
{
//search first letter in string s and if found in string str ,replace it with 2nd letter of s ie s[1]
if (s[0] == str[j])
{
str[j] = s[1];
++count;
}
}
return count;
}
/*output
Testing string functions
Entered string is : Testing string functions
Replaced string is : ------- ------ ---------
Enter letter to serach followed by letter to replace: sp
letter s found 3 times in this string .Serach letter p and after search and repl
ace string is : Tepting ptring functionp
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.