Sunday, November 28, 2010

Write a program that inputs n names and outputs them in alphabetical order.

#include
#include

static char *ch[20],temp[20];

static int i,j,k,n;
char Display(char *ch[]);//function Prototype
char sortwords(char *ch[])//fuction definition
{
printf("\n\n-----------------------\n\tBefor sort\n----------------------");
Display(ch);
for(i=0;i {
for(j=i+1;j {
if(strcmp(ch[i],ch[j])>0) // if ith index string>jth index
{
strcpy(temp,ch[j]);//swapping
strcpy(ch[j],ch[i]);
strcpy(ch[i],temp);
}
}
}
printf("\n\n-------------------------\n\tAfter sort\n------------------------");
}
main()
{
int n,i;
char ch;
printf("Enter the NO. of string:");
scanf("%d",&n);//no of string that we enter

for(i=0;i {
ch[i]=((char *) malloc(50*sizeof(char)));
scanf("%s",ch[i]);
}
sortwords(ch);//function calling
}//End of main