• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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.
0
romaniup
Asked:
romaniup
2 Solutions
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now