Sunday, December 5, 2010

Sorting the Strings

//THIS PROGRAM WILL SORT A LIST OF STRINGS IN ASCENDING ORDER

#include <iostream.h>

#include <conio.h>

#include <string.h>

void main()

{

    char names[5][31], t[31];

    int i, j;

    clrscr();

    //Accept 5 strings

    cout << "\nEnter 5 names : ";

    for(i=0; i<5; i++)

        cin.getline(names[i],31);

    //Sorting begins here

    for(i=0; i<4; i++)

    {

        for(j=i+1; j<5; j++)

        {

            if( strcmp(names[i],names[j]) > 0 )

            {

                strcpy(t,names[i]);

                strcpy(names[i],names[j]);

                strcpy(names[j],t);

            }

        }

    }

    //Display Sorted Strings

    cout << "\n5 names : \n";

    for(i=0; i<5; i++)

        cout << names[i] << endl;

getch();

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home