Convert String

I am trying to convert 46°23'39" to 46d23m39s and not been able to do it. Would it be possible to do it without using character pointers and just by using string? Thanks a lot.
romaniupAsked:
Who is Participating?
 
rajeev_devinCommented:
Do this

string s = "46°23'39\"";
s.insert(s.find("°"), "d");
s.erase(s.find("°"), 1);

s.insert(s.find("'"), "m");
s.erase(s.find("'"), 1);

s.insert(s.find("\""), "s");
s.erase(s.find("\""), 1);
0
 
itsmeandnobodyelseCommented:
Or that:

void replaceAll(string& s, char co, char cn)
{
     int pos = 0;
     while ((pos = s.find(co, pos)) != string::npos)
            s[pos++] = cn;
}

    string s = "46°23'39\"";
    replaceAll(s, '°', 'd');
    replaceAll(s, '\'', 'm');   // '  \'  '
    replaceAll(s, '"', 's');     // '  "  '

Regards, Alex


int pos1 = s.find('°');
int pos2
s[2] = 'd';
s[
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.