Thread: Sorting Strings

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Sorting Strings

    I am trying to sort names .I don,t really know whats wrong with the
    below code. I will appreciate any help.Thanks.

    Code:
        char copy_name[sir_count];
        int j;
        for( counter = 0 ; counter < sir_count + 1; counter++ ) {
            for( j = counter + 1 ; j < sir_count ; j++ ) {
                if ( strcmp( storeNames[counter], storeNames[j]) > 0 ) {
                    strcpy( copy_name, storeNames[counter] );
                    strcpy( storeNames[counter], storeNames[j] );
                    strcpy(  storeNames[j], copy_name );
                }
            }
        }
        printf("The Sirs are: ");
        for( counter = 0 ; counter < sir_count + 1; counter++ ) {
            printf("%s",storeNames[counter]);
            printf(" ");
        }
        printf("%d",sir_count);

    I have strings
    PAUL NINA FRANK

    If sorted, it should be like this
    FRANk NINA PAUL

    but I am geting this
    FRANK PAUL NINA

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I have sorted it out .
    Thanks .

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Nice pun

    It might be good to post what you did, just in case there was a mistake that just happened to appear to work, and so someone might point it out to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Memorize this - work with it, sleep with it, eat with it, play with it:

    Array index's begin with 0 in C, AND THEY END WITH the size of the array minus one.

    j < sir_count + 1, takes you out of bounds, unless that value is itself, less than the array size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting strings
    By yacek in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 02:07 PM
  2. sorting strings
    By sureshhewa in forum C Programming
    Replies: 8
    Last Post: 07-30-2008, 10:58 AM
  3. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  4. Sorting Strings
    By Derek5272 in forum C++ Programming
    Replies: 40
    Last Post: 08-24-2003, 10:12 PM
  5. Sorting Strings
    By SteelCityKid in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 01:14 PM