Google News
logo
C Program to Sort set of strings in Alphabetical Order
The following program user would be asked to enter a set of Strings and the program would sort and display them in ascending alphabetical order.

C Program – Sorting of a Set of Strings in Ascending alphabetical order :

Program :
#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings you 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;
}
Output :
How many strings you are going to enter?: 
5
Enter Strings one by one: 
Ramana
Suresh
Venkat
Srinu
Anu
Order of Sorted Strings:
Anu
Ramana
Srinu
Suresh
Venkat