Thursday

Write a program to input n names and sort them in alphabetical order.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char name[50][20],temp[20];
 int i,n,j;
 printf("\nEnter how many names: ");
 scanf("%d",&n);
 printf("Enter %d names\n",n);
 for(i=0;i<n;i++)
 scanf("%s",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("\nThe sorted names are\n");
 for(i=0;i<n;i++)
 printf("\n%s",name[i]);
 getch();
 }

No comments:

Post a Comment

Rahul-Notes