Answer to Question #132605 in C++ for Dan

Question #132605
Modify songVerse to play "The Name Game" (see OxfordDictionaries.com), by replacing "(Name)" with userName but without the first letter.

Ex: If userName = "Kaitlin" and songVerse = "Banana-fana fo-f(Name)!", the program prints:
Banana-fana fo-faitlin!
Ex: If userName = "Kaitlin" and songVerse = "Fee fi mo-m(Name)", the program prints:
Fee fi mo-maitlin
Note: You may assume songVerse will always contain the substring "(Name)".
1
Expert's answer
2020-09-11T12:17:54-0400
#include <iostream>
#include <string>

int main()
{
	const std::string placeholder = "(Name)";
	
	std::string songVerse, userName;
	std::cout << "Enter Song Verse: ";
	std::getline(std::cin, songVerse);
	std::cout << "Enter User Name: ";
	std::getline(std::cin, userName);

	std::string::size_type namePosition = songVerse.find(placeholder);

	if(namePosition == std::string::npos)
	{
		std::cout << "Error '" << placeholder << "(Name)""' not found\n";
	}
	else
	{
		std::string result = songVerse.replace(namePosition, placeholder.size()
											  ,userName.erase(0, 1));
		std::cout << result << "\n";
	}
	
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS