Write a 'C' Program to accept 5 names from user and store these names into an array. Sort these array elements in alphabetical order.


#include<stdio.h>
void main()
{
    char str[5][80],*temp;
    int  i,j;
    clrscr();
    printf("ENTER ANY 5 NAMES ::&gt;\n");
    for( i = 0 ; i &lt; 5 ; i++)
    {
        gets(str[i]);
    }
    printf("NAMES IN ALPHABETICAL ORDER ::&gt;\n");
    for(i = 0 ; i &lt; 5 ; i++)
    {
        for( j = 0 ; j &lt; 5 ; j++)
        {
            if(strcmp(str[i],str[j]) &lt; 0)
            {
                strcpy(&amp;temp[0],&amp;str[i][0]);
                strcpy(&amp;str[i][0],&amp;str[j][0]);
                strcpy(&amp;str[j][0],&amp;temp[0]);
            }
        }
    }
    for(i = 0 ; i &lt; 5 ; i++)
    {
        printf("%s\n",str[i]);
    }
    getch();
}
/*OUTPUT::
ENTER ANY 5 NAMES ::&gt;
Sachin
Zaheer
Harbhajan
Yuvraj
Dhoni

NAMES IN ALPHABETICAL ORDER ::&gt;
Dhoni
Harbhajan
Sachin
Yuvraj
Zaheer    */
Write a 'C' Program to accept 5 names from user and store these names into an array. Sort these array elements in alphabetical order. Write a 'C' Program to accept 5 names from user  and store these names into an array. Sort these array elements in alphabetical order. Reviewed by on April 24, 2015 Rating: 5
Powered by Blogger.