C Program to Sort Set of Strings in Alphabetical Order

C Program to Sort Set of Strings in Alphabetical Order:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
#include<string.h>

main()
{
   char a[5][20],b[20];
   int i,j;

   printf("Enter any five strings : ");
   for(i=0;i<5;i++)
   scanf("%s",a[i]);

   for(i=1;i<5;i++)
    {
      for(j=1;j<5;j++)
      {
         if(strcmp(a[j-1],a[j]) > 0)
         {
            strcpy(b,a[j-1]);
            strcpy(a[j-1],a[j]);
            strcpy(a[j],b);
         }
      }
    }

   printf("\nString alphabetical in order are : ");
   for(i=0;i<5;i++)
    printf("\n%s",a[i]);
}

OUTPUT:

Enter any five strings : Baron Tuts Easy Best Learning

String alphabetical in order are :
Baron
Best
Easy
Learning
Tuts

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.