Thread: Sorting strings in an array

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Sorting strings in an array

    Hi everyone,
    Im trying to sort my array of student details by registration number. I have no idea how to go about it, but here is my code so far if anyone can help.

    [CODE]
    #include <string.h>
    #include <stdio.h>

    char seps[] = " , ";
    char *token;

    typedef enum Menu { FIRST = 1, NEXT, PREVIOUS, LAST, SORT, EXIT };

    typedef struct
    {
    char fName[10];
    char lName[10];
    char regNo[9];
    char doB[30];} PERSON;

    PERSON student[10];

    studenttemp[20];

    EnterChoice(); // display main menu
    void firstRec(); // view First record
    void nextRec(); // view Next record
    void previousRec(); // view Previous record
    void lastRec(); // view Last record
    void sortRec(); // Sort records by Registration Number

    char choice;
    int i;
    int counter;
    //-----------------------------------------------------------------------------------
    int main()
    {
    char s[100];
    FILE * f;
    int len, i;
    i=-1;
    f = fopen("c:\\students.txt","r");

    for ( ; ; ) {
    fgets(s, 100, f);
    if feof(f) break;
    len = strlen(s);
    if (len) { s[len] = 0;}
    if (i>=9) break;
    i++;
    {
    token = strtok( s, seps ); strcpy(student[i].fName, token);
    token = strtok( NULL, seps ); strcpy(student[i].lName, token);
    token = strtok( NULL, seps ); strcpy(student[i].regNo, token);
    token = strtok( NULL, seps ); strcpy(student[i].doB, token);

    }
    }

    fclose(f);



    while ( ( choice = EnterChoice() ) != EXIT ) // menu loop
    {
    switch ( choice )
    {
    case FIRST: firstRec(); break;
    case NEXT: nextRec(); break;
    case PREVIOUS: previousRec(); break;
    case LAST: lastRec(); break;
    case SORT: sortRec(); break;
    }
    }
    }
    //------------------------------------------------------------------------------------
    EnterChoice()
    {
    printf( "\nEnter your choice\n"
    "1 - View first record\n"
    "2 - View next record\n"
    "3 - View previous record\n"
    "4 - View last record\n"
    "5 - Sort records by Registration Number\n"
    "6 - End program\n? " );
    scanf( "%d", &choice );
    return choice;
    }

    void firstRec()
    {
    counter=0;
    printf("\nFirst Name: %s\n\n", student[counter].fName);
    printf("Last Name: %s\n\n", student[counter].lName);
    printf("Registration Number: %s\n\n", student[counter].regNo);
    printf("Date of Birth: %s\n", student[counter].doB);
    }
    void nextRec()
    {
    ++counter;
    if (counter >=10)
    {
    printf("\nThere are no more records to view!\n\n");
    }
    else
    {
    printf("\nFirst Name: %s\n\n", student[counter].fName);
    printf("Last Name: %s\n\n", student[counter].lName);
    printf("Registration Number: %s\n\n", student[counter].regNo);
    printf("Date of Birth: %s\n", student[counter].doB);
    }
    }
    void previousRec()
    {
    --counter;
    if (counter <=-1)
    {
    printf("\nThere are no more records to view!\n\n");
    }
    else
    {
    printf("\nFirst Name: %s\n\n", student[counter].fName);
    printf("Last Name: %s\n\n", student[counter].lName);
    printf("Registration Number: %s\n\n", student[counter].regNo);
    printf("Date of Birth: %s\n", student[counter].doB);
    }
    }
    void lastRec()
    {
    counter=9;
    printf("\nFirst Name: %s\n\n", student[counter].fName);
    printf("Last Name: %s\n\n", student[counter].lName);
    printf("Registration Number: %s\n\n", student[counter].regNo);
    printf("Date of Birth: %s\n", student[counter].doB);
    }

    void sortRec() // Sort records by Registration Number
    {

    printf("\nRecords now sorted by Registration Number.\n\n");
    }
    [CODE]

    cheers
    John

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    TAGS!!!!!!
    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.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Doesn't anyone use the preview button?
    How do you know your post is correct unless you use the Preview button?

    Use the Preview Button!!!!
    It is there for a purpose, not just to give
    the submit button a friend to hang out with!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    hi,

    as u have declared the regNo as const char * u can use the strcmp function and sort the records as -

    Code:
    void sortRec() // Sort records by Registration Number
    {
    	int i,j;
    	PERSON tp;
    	printf("\nRecords now sorted by Registration Number.\n\n");
        for(i=0;i<9;i++)
    	{
    		for(j=i+1;j<10;j++)
    		{
    			if(strcmp(student[i].regNo,student[j].regNo) > 0) 
    			{
    				tp=student[i];
    				student[i]=student[j];
    				student[j]=tp;
    			}
    		}
    	}
    	printf("\nFirst Name  Last Name  Registration Number  DOB\n"); 
    	for(i=0;i<10;i++)
    	{
    		printf("\n%s ", student[i].fName);
    		printf("%s ", student[i].lName);
    		printf("%s ", student[i].regNo);
    		printf("%s", student[i].doB);
    	}
    }
    use this code in ur prgm and u prgm will run sucessfully. do not give up. think laterally and get the ideas.

    bye
    Praveen

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Hey Praveen,
    Thanks very much man.
    Thanks a Million!

    Its all become so clear to me now!

    Dont want to be cheeky, but any chance you could help me with my final problem?
    I want the sort function to sort the registration numbers by 97, 98, 99, 00, 01 etc. at the moment it works by 00, 01, 02, 97, 98, 99 etc. Any ideas?

    Cheers
    and thanks again.

    John

    If I can ever help you just drop me a line:
    [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Sorting Array
    By dmkanz07 in forum C Programming
    Replies: 14
    Last Post: 04-24-2007, 10:12 AM
  4. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  5. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM