Sort chars

This is a discussion on Sort chars within the C++ Programming forums, part of the General Programming Boards category; I'm just trying to figure out if there is any way i can switch 2 string of characters like numbers ...

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Sort chars

    I'm just trying to figure out if there is any way i can switch 2 string of characters like numbers with swap function. Somehow, i'm getting application error when trying to sort the characters in alphabetical oder with this:

    Code:
    vector<char*>Name;
    //push some char into the vector array
    ..............................
    
    
    for(int j=1; j<Name.size(); j++)
    {  
        for(int i=0; i<Name.size()-1; i++)
         {
              if( strcmp(Name[i], Name[k]) > 0)
               {
                     char* temp;
                     strcpy(temp, Name[i]);
                     strcpy(Name[i], Name[k]);
                     strcpy(Name[k], temp);
               }
         }
    }
    Am i doing it right or is there anyway i can sort the characters in alphabetical order
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    char like int is represented in binary just like everything else. To make it more obvious, characters are mapped to the keyboard numerically. I'm certain any comparison sort algorithm will be able to sort characters like it does numbers. Just make sure that your functions accept pointer to char, and that should solve most your problem.
    Last edited by whiteflags; 04-30-2006 at 10:31 PM. Reason: Boldness and now spelling

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > vector<char*>Name
    OK, you used one data type which takes care of itself, and then use another where you have to do all the work yourself.
    Why not use vector<string> instead, then at least you don't have to worry about all the string allocation problems.

    Also, if you don't need to write your own sort function, use a standard one
    This should give you the ideas for sorting a vector<string>
    pointers and vectors
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Cool, i found it pretty much like this:
    Code:
    #include <algorithm>
    vector<string>Name;
    //push some into the vector
    
    sort(Name.begin(), Name.end());
    //the job is done
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21