BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Sort set of strings in alphabetical order

Last Updated: February 10, 2015 by Chaitanya Singh | Filed Under: C Programs

In 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

/* 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;
}

Output:
sorted_strings_ascending

As you can observe in the above screenshot of output that we have entered 5 strings and the program then sorted them in ascending order. We got a sorted set of strings as output.

Top Related Articles:

  1. C Program to concatenate two strings without using strcat
  2. Selection Sort Program in C
  3. Ultimate Collection of C Programs: Source Code with Outputs
  4. C Program to print the day for an input of date month and year
  5. C Program to Access Array Elements Using Pointer

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap