C

C Program To Sort A Set Of Strings Alphabetically

The user would be asked to enter a set of Strings in the following program, and the program would sort and display them in ascending alphabetical order.

C Program – Sorting a String Set in Ascending Alphabetic Order

/* This program would sort the input strings in
 * an ascending order and would display the same
 */
#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings u are going to enter?: ");
   scanf("%d",&count);

   puts("Enter Strings one by one: ");
   for(i=0;i<=count;i++)
      gets(str[i]);
   for(i=0;i<=count;i++)
      for(j=i+1;j<=count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("Order of Sorted Strings:");
   for(i=0;i<=count;i++)
      puts(str[i]);
   
   return 0;
}

As you can see in the output screenshot above, we entered 5 strings and the program sorted them in ascending order. As a result, we received a sorted set of strings.

Related Articles

Leave a Reply

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

Back to top button