This assignment will access your C++ language skills. After completing this assi
ID: 3900191 • Letter: T
Question
This assignment will access your C++ language skills. After completing this assignment, you will be able to do the following:
(1) prompt a user for input, (2) write output to the screen and (3) use the string class
Write a program that have two functions:
1. A boolean function called palindrome that accepts one string argument and returns true if the string reads the same forward as it does backwards. For example, madam, 463364, and “ABLE WAS I ERE I SAW ELBA”, are all palindromes.
2. A function called replace_all that accepts three string arguments, old_substring, new_substring, and original_string. The function will return a copy of string “original_string” with each occurrence of old_substring replace by new_substring. “original_string” is not changed. For example is original_string = “aaabbacceeaaa”, old_substring = “aa”, and new_substring = “zzz”, the function will return the string “zzzabbacceezzza”. Consider the following prototypes for the functions:
bool palindrome(constr string s);
string replace_all(const string & original_string, const string & old_substring, const string & new_substring);
Please use substr and find.
Call this “program3_strings.cpp”.
Consider the following skeleton to help you implement your program:
////////////////////////////////////////////////////////////////////////////////////////////
//Remember to include a program header
///////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
bool palindrome(const string &);
string replace_all(const string & original_string, const string & old_substring, const string & new_substring);
////////////////////////////////////////////////////////////////////////////////////////////
//Remember to include a function header
//Function name:
//Precondition
//Postcondition
//Description
///////////////////////////////////////////////////////////////////////////////////////
bool palindrome(const string & s)
{
//your code goes here
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//Remember to include a function header
//Function name:
//Precondition
//Postcondition
//Description
///////////////////////////////////////////////////////////////////////////////////////
string replace_all(const string & original_string, const string & old_substring, const string & new_substring)
{
string s;
//your code goes here
return s;
}
int main()
{
//driver should have a loop prompting user for input. For example, (1) user wants to run the function palindrome or
//replace_all and (2) prompt the user for the appropriate input once the function has been determine (one string for
//palindrome and 3 for replace_all).
return 0;
}
Please use substr and find.
Explanation / Answer
solution:
#include<iostream>
#include<string>
using namespace std;
bool palindrome(const string & s);
string replace_all(const string & original_string, const string &
old_substring, const string & new_substring);
bool palindrome(const string & s)
{
int len = s.length();
string ns;
int c;
for(c = len - 1; c >= 0; c--)
{
ns = ns + s[c];
}
if(s.compare(ns) == 0)
return true;
else
return false;
}
string replace_all(const string & original_string, const string &
old_substring, const string & new_substring)
{
string nw = original_string;
size_t pos = 0;
while ((pos = nw.find(old_substring, pos)) != std::string::npos)
{
nw.replace(pos, old_substring.length(), new_substring);
pos += new_substring.length();
}
return nw;
}
int main()
{
char ch;
int op;
string pa;
string st, ods, nes;
bool res;
do
{
cout<<" Enter 1 for Palindrome";
cout<<" Enter 2 for Replace string";
cout<<" Enter 3 for Exit ";
cin>>op;
if(op == 1)
{
cout<<" Enter a string to check Palindrome" ;
cin>>pa;
res = palindrome(pa);
if(res)
cout<<" Entered string = "<<pa<<" is Palindrome";
else
cout<<" Entered string = "<<pa<<" is not a Palindrome";
}
else if(op == 2)
{
cout<<" Enter the original String";
cin>>st;
cout<<" Enter the searched String";
cin>>ods;
cout<<" Enter the string to Replace";
cin>>nes;
cout<<" After Replacement = "<<replace_all(st, ods, nes);
}
else if(op == 3)
exit(0);
else
cout<<" Wrong choice";
}while(1);
}