Discussion Forums

New User? Please read instructions!

C program to sort given names in alphabetical order  

  RSS

thiyagarajan
Eminent Member
Joined: 8 months  ago
Posts: 26
27/01/2017 9:38 am  

C program to sort given names in alphabetical order


ReplyQuote
admin
Eminent Member
Joined: 5 years  ago
Posts: 26
01/02/2017 4:44 am  

Hi, Below C program will sort inputs 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("Please enter how many number of names to be sorted in alphabetical order\n");

   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]);

   }

   

}

Output:

Please enter how many number of names to be sorted in alphabetical order

Please enter 4 names one by one

thiyagu

raja

mani

Arul

Entered names before sorting in alphabetical order

thiyagu

raja

mani

Arul

Entered names after sorting in alphabetical order

Arul

mani

raja

thiyagu

Edited: 8 months  ago

ReplyQuote
  
Working

Please Login or Register

1. Online Compilers - Compile & Run your C/C++ programs:

✅ Online Compilers

2. Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!