Thursday, 21 June 2012

write a c program to accept names from user and store these name in an array & sort the array element in ascending order


/* write a c program to accept names from user and store these name
in an array & sort the array element in ascending order*/
#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][10],temp[20],i,j;
int n;
clrscr();
printf("\nhow many number :");
scanf("%d",&n);
printf("\nenter the name :");
for(i=0;i<n;i++)
{
flushall();
gets(name[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\nsorted name is :\n");
for(i=0;i<n;i++)
{
puts(name[i]);
}
getch();
}
/*

how many number :3                                                            
                                                                             
enter the name :peer                                                          
santosh                                                                      
rahul                                                                        
                                                                             
sorted name is :                                                              
peer                                                                          
rahul                                                                        
santosh                                                                      
                                                                             
   */

No comments: