Program in C to sort strings in ascending order

Write a program in C to sort strings in ascending order

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
            int i,j,n,x;
            char str[20][20],str1[20][20];
            clrscr();
            printf(“enter the number of strings:\n”);
            scanf(“%d”,&n);
            for(i=0;i<n;i++)
            {
                        printf(“\nenter str[%d]”,i+1);
                        scanf(“%s”,&str[i]);
            }
            for(i=0;i<n;i++)
            {
                        for(j=i+1;j<n;j++)
                        {
                                    x=strcmp(str[i],str[j])
                                    if(x>0)
                                    {
                                                strcpy(str[1],str[j]);
                                                strcpy(str[j],str[i]);
                                                strcpy(str[i],str[1]);
                                    }
                        }
            }
            printf(“\nthe sorted strings in ascending order is\n”);
            for(i=0;i<n;i++)
            {
                        printf(“\n%s”,str[i]);
            }
            getch();
}


Post a Comment