Question

5.9.2: Modify a string parameter. C++ Complete the function to replace any period by an exclamation...

5.9.2: Modify a string parameter. C++

Complete the function to replace any period by an exclamation point. Ex: "Hello. I'm Miley. Nice to meet you." becomes:

"Hello! I'm Miley! Nice to meet you!"

#include <iostream>
#include <string>
using namespace std;

void MakeSentenceExcited(string& sentenceText) {

/* Your solution goes here */

}

int main() {
string testStr;

getline(cin, testStr);
MakeSentenceExcited(testStr);
cout << testStr;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer:

#include <iostream>
#include <string>
using namespace std;

void MakeSentenceExcited(string& sentenceText) {

for(int i=0; i<sentenceText.length(); i++)
{
if(sentenceText[i]=='.')
{
sentenceText = sentenceText.substr(0, i)+"!"+sentenceText.substr(i+1, sentenceText.length());
}
}
  

}

int main() {
string testStr;

getline(cin, testStr);
MakeSentenceExcited(testStr);
cout << testStr;

return 0;
}

Here is the output:

Hope you like it!

Please provide comments if you have any doubts!

Add a comment
Know the answer?
Add Answer to:
5.9.2: Modify a string parameter. C++ Complete the function to replace any period by an exclamation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence;...

    Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence; std::getline(std::cin, sentence); // manipulate the sentence here std::cout << sentence << "\n"; }} Lab 4.5.1 Text manipulation: duplicate white space Objectives Familiarize the student with: • the basics of string and text manipulation. Scenario Write a program that will read a line of text and remove all duplicate whitespace. Note that any single whitespace characters must remain intact. #include <string> #include <iostream> int main()...

  • In C++ Write a program that will read a string, call 2 functions to modify the...

    In C++ Write a program that will read a string, call 2 functions to modify the string, and then print the final result. The first function should take a string parameter and return the string without any vowels. The second function should take a string and double every letter (which should be all consonants at this point). Be sure to: You must use more than [ ] and at You must use erase, insert, replace, find, and/or substr to make...

  • CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar...

    CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }

  • For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling...

    For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling the functions relating to our book struct correctly. The issues lie in main(). Here is the .cpp: #include "book.h" void add_author(Book& book){         if (book.num_auth==3){             std::cout<<"\nA book can't have more than 3 authors!";         }         else{             std::cout<<"\nAdd Author: ";             getline(std::cin, book.authors[book.num_auth]);             book.num_auth++;         } }; void remove_last(Book& book){         if(book.num_auth<=1){             std::cout<<"\nA book must have at least 1 author!";...

  • CHALLENGE ACTIVITY 13.1.2: Basic function call. Complete the function definition to output the hours given minutes....

    CHALLENGE ACTIVITY 13.1.2: Basic function call. Complete the function definition to output the hours given minutes. Output for sample program: 3.5 1 test passed All tests passed 1 #include <iostream> 2 using namespace std; 3 4 void OutputMinutesAsHours (double origMinutes) { 5 6 /* Your solution goes here */ 7 8} 9 10 int main() { 11 double minutes; 12 13 cin >> minutes; 14 15 OutputMinutesAsHours (minutes); // Will be run with 210.0, 3600.0, and 0.0. 16 cout <<...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • So Im here and im stuck. I need to figure out how to get the character...

    So Im here and im stuck. I need to figure out how to get the character of the first name to be printed on a separate line and the middle and last. is there a way where i can tell a my function to stop once it hit " "? Please help. I #include iostream 2 #include string using namespace i; string name 7 getline cin, name 4 int main ) cout<"Please enter your full name: " problem3.cpp 3Write a...

  • Complete the second PrintSalutation function to print the following given personName "Holly' and customSalutation "Welcome": Welcome,...

    Complete the second PrintSalutation function to print the following given personName "Holly' and customSalutation "Welcome": Welcome, Holly End with a newline. 1 #include <iostream> 2 #include <string> 3 using namespace std; 4. 5 void PrintSalutation(string personName) { 6 cout << "Hello, << personName << endl; 7} 8 9 // Define void PrintSalutation(string personName, string customSalutation)... 10 11 y. Your solution goes here 12 13 int main() { 14 PrintSalutation("Holly", "Welcome"); 15 PrintSalutation("Sanjiv"); 16 17 return ; 18 ) Run

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT