In: Computer Science
Use the ListInterface operations only to create a tackPunct routine in C++ that will take a list of string (array or linked, your choice) as a reference. Use the list operations on the passed list to make all items that don’t have a ‘.’, ‘?’ or ‘!’ have a ‘.’ tacked on.
Program
#include <iostream>
#include <list>
#include <iterator>
#include <string>
using namespace std;
//function tackPunct to make all items that don’t
//have a ‘.’, ‘?’ or ‘!’ have a ‘.’ tacked on
void tackPunct(list <string> &slist)
{
list <string> :: iterator it;
for(it = slist.begin(); it != slist.end(); ++it)
{
string s = *it;
int len = s.size();
if(s[len-1]!='.' && s[len-1]!='?' &&
s[len-1]!='!')
*it = *it + ".";
}
}
//main function
int main()
{
list <string> slist;
slist.push_back("Hello");
slist.push_back("World");
slist.push_back("Welcome!");
tackPunct(slist);
list <string> :: iterator it;
for(it = slist.begin(); it != slist.end(); ++it)
{
cout<<*it<<endl;
}
return 0;
}
Output:
Hello.
World.
Welcome!
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.