Using std::string
should help you tremendously as you can leverage its search and replace functionality. You'll want to start with the find
function which allows you to search for a character or a string and returns an index where what you are searching for exists or npos
if the search fails.
You can then call replace
passing it the index returned by find
, the number of characters you want to replace and what replace the range with.
The code below should help you get started.
#include <string>
#include <iostream>
int main()
{
std::string roman("IVV");
// Search for the string you want to replace
std::string::size_type loc = roman.find("VV");
// If the substring is found replace it.
if (loc != std::string::npos)
{
// replace 2 characters staring at position loc with the string "X"
roman.replace(loc, 2, "X");
}
std::cout << roman << std::endl;
return 0;
}
std::string
and leverage it'sfind
andreplace
member functions.