Write a ‘C’ program to accept 5 names from user, store these names into array and sort these array elements in alphabetic order.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 int i,j,d;
 char ch[20][30],tmp[30];
 clrscr();
 printf("\n enter the no of string\n");
 scanf("%d",&d);
 printf("\n entert %d string",d);
 for(i=0;i<=d;i++)
 {
 gets(ch[i]);
 printf("\n\n\n");
 printf("\nenter string are\n");
 }
 for(i=0;i<=d;i++)
 {
 printf("\n%s",ch[i]);
  }
  for(i=0;i<=d;i++)
  {
  for(j=i+1;j<=d;j++)
  {
  if(strcmp(ch[i],ch[j])>0)
  {
  strcpy(tmp,ch[i]);
  strcpy(ch[i],ch[j]);
  strcpy(ch[j],tmp);
  }
  }
  }
  printf("\n\n\n");
  printf("\n sorted string are:");
  for(i=0;i<=d;i++)
  printf("\n%s",ch[i]);
  getch();
  }

Comments

Post a Comment