Thread: Alphabetize String

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Alphabetize String

    Hi,

    I was just wondering if there's a function out there that will alphabetize all of the letters in a string. For example, if I typed in the string "I love New York" the function should change it to

    Code:
       eeiklnoorvwy
    Thanks, I just need it for a piece of my program.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It's a one-liner.

    Code:
    std::sort(str.begin(), str.end());
    Provided the data is in a std::string, not a C-style string. It can work on C-style strings too:

    Code:
    std::sort(str, str + strlen(str));
    Last edited by brewbuck; 11-09-2007 at 04:13 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    sorry new to c++, but would i just replace 'string' with whatever the input is, and the second 'string' with the variable for the output?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by wco5002 View Post
    sorry new to c++, but would i just replace 'string' with whatever the input is, and the second 'string' with the variable for the output?
    No. It actually sorts the string in place, destroying the original. To keep the original but get a sorted version, make a copy first:

    Code:
    std::string sorted_string = str;
    std::sort(sorted_string.begin(), sorted_string.end());
    You also need to #include <algorithm> to get the std::sort function.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    str is the name of the string you want to sort. It's both the input and the output.
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    int main() {
        std::string str = "I love New York";
    
        std::cout << str << std::endl;
        std::sort(str.begin(), str.end());
        std::cout << str << std::endl;
    }
    Something like that.

    Note that that won't convert the letters to lowercase. You'll have to do that yourself if you want to -- use tolower() or toupper() from <cctype>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    does it account for uppercase and lower case letter?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by wco5002 View Post
    does it account for uppercase and lower case letter?
    It treats any uppercase letter as coming "before" any lowercase letter. If you want it to disregard case, you need to write a special comparator.

    Code:
    struct compare_no_case
    {
        bool operator()(char a, char b)
        {
            return tolower(a) < tolower(b);
        }
    };
    
    std::sort(str.begin(), str.end(), compare_no_case());
    Last edited by brewbuck; 11-09-2007 at 04:28 PM.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    thanks for the pointers, got everything working good now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM