Convert String

Posted on 2006-05-22
Last Modified: 2010-04-01
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.
Question by:romaniup
    LVL 12

    Accepted Solution

    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);
    LVL 39

    Assisted Solution

    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';

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now