When comparing sentences we are often not interested in differences in case (UPP
ID: 3870161 • Letter: W
Question
When comparing sentences we are often not interested in differences in case (UPPERCASE vs lowercase) or punctuation. For this problem, you will be writing a function that takes two string parameters- sentence and punctuation and returns the same string in lowercase (remember the built- in tolower function) and with given punctuation removed. You must use nested loops. The built lowerNoPunctuation returns a string and takes a two strings as parameters: string lowe rNoPunctuation(string str, string punch) //Your code here Example Output: string str "Hello, my name is Inigo Montoya. You killed my father. Prepare to die! string punct",?" string newStr = lowerN°Punctuation (str, punct) coutExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
string lowerNoPunctuation(string str, string punch);
int main()
{
string str = "Hello, my name is Inigo Montoya. You killed my my father. Prepare to die!";
string punct = ".,:!?";
string newStr = lowerNoPunctuation(str, punct);
cout << newStr << endl;
}
string lowerNoPunctuation(string str, string punch)
{
string newStr;
int punct = 0;
char ch;
for (int i = 0; i < str.length(); i++)
{
for (int j = 0; j < punch.length(); j++)
{
if (str[i] == punch[j])
{
punct = 1;
break;
}
else
{
punct = 0;
}
}
if (!punct)
{
ch = str[i];
newStr.push_back(tolower(ch));
}
}
return newStr;
}
--------------------------------------------------------------------------------------------------------------------------
//output
same as result given in problem
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.