Sorting Strings in Ascending Order in C


Write an interactive C program to arrange the given 5 strings in alphabetical order. [Dec 2014, Set-3]

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

int main()
{
    char str[5][80]; 
    char temp[80];
    int i,j;
	
    for (i = 0; i < 5; i++)
    {
        printf("Enter string %d : ", i + 1);
        gets(str[i]);
    }

    /* Sorting strings */
	
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < (4-i); j++)
        {
            if (strcmp(str[j], str[j + 1]) > 0)
            {
                strcpy(temp, str[j]);
                strcpy(str[j], str[j + 1]);
                strcpy(str[j + 1], temp);
            }
        }
    }
	
    printf("\nAfter sorting\n");
    for (i = 0; i < 5; i++)
    {
        puts(str[i]);
    }

    return 0;
}