C Program for sorting the list of Strings

February 7, 2025 No Comments » Hits : 75






C Program for sorting the list of names

Write a C program which will accept multiple strings fron the user and will sort them in ascending order.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
    char *string1[5],*temp;
    int i,j,n;
    clrscr();
    printf("\nHow many names do you want to have?");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter the name %d: ",i);
        flushall();
        gets(string1[i]);
    }
    for(i=0;i<n;i++)
    {
     for(j=0;j<n-1;j++)
     {
       if(strcmp(string1[j],string1[j+1])>0)
         {
         strcpy(temp,string1[j]);
         strcpy(string1[j],string1[j+1]);
         strcpy(string1[j+1],temp);
         }
     }
    }
    printf("\nSorted List : ");
    flushall();
    for(i=0;i<n;i++)
        puts(string1[i]);
    getch();
}

Related Articles:

Leave A Response