Program to Sort Strings in alphabetical order

void main()
{
int i,j,n;
char s[5][20],t[20];

printf("\n Enter any 5 strings:");

for(i=0;i<5;i++) //Taking input strings
{
scanf("%s",s[i]);
}

for(i=1;i<5;i++) // Logic to sort the strings alphabetiaclly
{
for(j=1;j<5;j++)
{
if( strcmp( s[j-1] , s[j] ) > 0)
{
strcpy( t,s[j-1] );
strcpy( s[j-1] , s[j] );
strcpy( s[j] , t);
}
}
}

printf("\n The Strings in the order:");
for(i=0;i<5;i++) // Display the strings
{
printf("\n %s \n",s[i]);
}
}

Comments

  1. Pandhare Dipali12:29 PM

    /*program to sort strings in alphabetical order*/
    #include
    void main()
    {
    int i,j,n;
    char s[5][20],t[20];

    printf("\n Enter any 5 strings:");

    for(i=0;i<5;i++)
    {
    scanf("%s",s[i]);
    }

    for(i=1;i<5;i++)
    {
    for(j=1;j<5;j++)
    {
    if( strcmp( s[j-1] , s[j] ) > 0)
    {
    strcpy( t,s[j-1] );
    strcpy( s[j-1] , s[j] );
    strcpy( s[j] , t);
    }
    }
    }

    printf("\n The Strings in the order:");
    for(i=0;i<5;i++)
    {
    printf("\n %s \n",s[i]);
    }
    }

    ReplyDelete

Post a Comment