Write a program to read an array of names and sort them in alphabetical order.
#include
#include
#include
void main()
{
char str[5][10],t[10];
int i,j;
clrscr();
printf("\nEnter the strings : ");
for(i=0;i<5;i++)
scanf("%s",str[i]);
printf("\nSorted strings are:\n");
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(t,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],t);
}
}
printf("\n%s",str[i]);
}
getch();
}
/*Output
Enter the strings : rose
mango
apple
marigold
banana
Sorted strings are:
apple
banana
mango
marigold
rose*/
#include
#include
#include
void main()
{
char str[5][10],t[10];
int i,j;
clrscr();
printf("\nEnter the strings : ");
for(i=0;i<5;i++)
scanf("%s",str[i]);
printf("\nSorted strings are:\n");
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(t,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],t);
}
}
printf("\n%s",str[i]);
}
getch();
}
/*Output
Enter the strings : rose
mango
apple
marigold
banana
Sorted strings are:
apple
banana
mango
marigold
rose*/