Your Ad Here

Monday, March 2, 2009

Write a C++ program to sort a list of names in ascending order.

#include(iostream.h)
#include(conio.h)
#include(stdio.h)
#include(string.h)
void main()
{
char st[10][10],temp[10];
int i, j, n;
clrscr();
cout<<"Enter the no. of names:";
cin>>n;
cout<<"Enter the different names:";
for(i=0; i< n; i++)
cin>>st[i];
for(i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(strcmp(st[i], st[j+1]) >0)
{
strcpy(temp,st[i]);
strcpy(st[i],st[j+1]);
strcpy(st[j+1],temp);
}
}
}
cout<<"Given names after ascending order:";
for(i=0;i<5;i++)
cout<< st[i];
getch();
}


Output: Enter the no.of names: 5

Enter the different names: apple
cat
boy
apec
suresh

Given names after ascending order:
apec
apple
boy
cat
suresh

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

9 comments: