-3

I am making a roman numeral converter. I have everything figured out except there is one problem at the end.

The string looks like IVV

I need to make it IX

I have split the string at each new letter, then appended them back on, then using an if statement to see if it contains 2 "V"s. I want to know if there is a simpler way to do this.

5
  • 3
    show us some code dude.
    – 101010
    Jun 17, 2014 at 19:57
  • 1
    Use std::string and leverage it's find and replace member functions. Jun 17, 2014 at 20:01
  • You should post your code for searcing a solution
    – AngeloDM
    Jun 17, 2014 at 20:02
  • What part of the code do you need? all I have is a string that I need to replace certain characters in.
    – Achyut
    Jun 17, 2014 at 20:20
  • 1
    @Fusion You've beem requested to show what you have tried already! Just saying 'Gimme teh codez plz!' doesn't work well here :P ... Jun 17, 2014 at 20:31

2 Answers 2

2

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;
}
5
  • 1
    Same reason as I've been downvoting the other answer. Don't feed the help vampires! Jun 17, 2014 at 20:35
  • 2
    You didn't serial downvote on my account in response to this?!? Did you? Jun 17, 2014 at 21:45
  • 3
    Why would I? I totally disagree with you but I'm not a DB. Jun 17, 2014 at 21:59
  • OK, already thought so. I just was curious ... ('DB' BTW, I don't know this shortcut, would you mind enlightening me?) Jun 17, 2014 at 22:00
  • Ahhhh, I see. No you're certainly not! I mostly like and agree with your answers and comments. (I'm watching "Two and a half men" currently, there's where you can see DB's in action ;) ...) Jun 17, 2014 at 22:07
-2

You could use std string find and rfind operations, these find the position of the first and the last occurrence of the entered parameter, check if these are not equal and you will know

Answer updated

#include <string>

int main()
{
    std::string x1 = "IVV";
    if (x1.find('V') !=x1.rfind('V'))
    {
        x1.replace(x1.find('V'), 2, 'X');
    }
    return 0;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.