Sunday, April 5, 2020

Sort Given Names In Alphabetical Order | C Programming Example

C Program Example To Sort Given Names In Alphabetical Order


This Article From Learn c programming in Hindi Will help you to learn About "Sort Given Names In Alphabetical Order", C programming language Example and check out "selection sort program in c", "socket programming in c", "merge sort program in c","Kruskal's algorithm in c", "for loop c programming", "c struct example","simple program in c language", "simple CPP program", "c programming practice questions", "c program using for loop", "programming using c", "hello world program in c", "if statement in c ".


C programming Example
C programming Example

  • Source Code For C Programming Example : -


#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

0 Comments:

Post a Comment