C program to Sort Names in Alphabetical Order

C program to sort names in alphabetical order; Through this tutorial, we will learn how to sort names in alphabetical order in c program.

Algorithm to Sort Names in Alphabetical Order

Use the following algorithm to write a program to sort names in alphabetical orders; as follows:

  • Step 1: First, create an array and initialize with the values.
  • Step 2: For loop from i=0 to i 0):
  • Step 5: If yes, then swap(array[j], array[j+1])
  • Step 6: End nested loop.
  • Step 7: End external loop.
  • Step 8: Output sorted array

C program to Sort Names in Alphabetical Order

#include <stdio.h>

#include <string.h>

int main()

{

   int i, j, num;

   char name[20][10], t_name[15][10], temp[20];



   printf("How many number of names to be sorted in alphabetical order :- ");

   scanf("%d", &num);



   printf("Please enter %d names one by one\n", num);

   for(i=0; i< num ; i++)

   {

      scanf("%s",name[i]);

      strcpy (t_name[i], name[i]);

   }



   for(i=0; i < num-1 ; i++)

   {

      for(j=i+1; j< num; j++)

      {

         if(strcmp(name[i],name[j]) > 0)

         {

             strcpy(temp,name[i]);

             strcpy(name[i],name[j]);

             strcpy(name[j],temp);

         }

      }

   }



   printf("Names before sorting in alphabetical order\n");

   for(i=0; i< num ; i++)

   {

      printf("%s\n",t_name[i]);

   }



   printf("Names after sorting in alphabetical order\n");

   for(i=0; i< num ; i++)

   {

      printf("%s\n",name[i]);

   }



}

The output of the above c program; as follows:

How many number of names to be sorted in alphabetical order :- 4
Please enter 4 names one by one
my
class
was
done
Names before sorting in alphabetical order
my
class
was
done
Names after sorting in alphabetical order
class
done
my
was

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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