Connecting Tech Pros Worldwide Help | Site Map

c++ Prg to arrange the names in alphabetical order

King Robin's Avatar
Newbie
 
Join Date: Dec 2006
Location: India
Posts: 2
#1: Dec 6 '06
Hi all,

Tell me a c++ program to get 5 names and arrange it in alphabetical order.
Newbie
 
Join Date: Dec 2006
Posts: 2
#2: Dec 6 '06

re: c++ Prg to arrange the names in alphabetical order


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char name[5][20],temp[20];
for(int i=0;i<5;i++)
{
gets(name[i]);
}

for(i=0;i<5;i++)
{
for (int j=0;j<i-1;j++)

{
if (strcmp(name[j],name[j+1])>0);
{
strcpy(name[j],temp);
strcpy(name[j+1],name[j]);
strcpy(temp,name[j+1]);
}


}
}
for(i=0;i<5;i++)
{
puts(name[i]);
cout<<"\n";
}

getch();
}
King Robin's Avatar
Newbie
 
Join Date: Dec 2006
Location: India
Posts: 2
#3: Dec 7 '06

re: c++ Prg to arrange the names in alphabetical order


Hi

Thanks for ur program. I have a doubt.
In the following line

"char name[5][20]" does the

[5] refers to the array

[20] size of the array.
Reply