How to write a c program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order in C Programming Language ?
Solution:
/*C program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[10][20],temp[20];
int i,j,n;
clrscr();
printf("\nEnter number of strings: ");
scanf("%d",&n);
printf("\nEnter the strings: ");
for(i=0;i<=n;i++)
{
gets(c[i]);
}
puts(c[i]);
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(c[i],c[j])>0)
{
strcpy(temp,c[i]);
strcpy(c[i],c[j]);
strcpy(c[j],temp);
}
}
}
printf("\nSorted list is:\n");
for(i=0;i<=n;i++)
printf("\n%s",c[i]);
getch();
}
//OUTPUT:
//Enter the number of strings: 3
//Enter the strings: VINIT
//RAVI
//SURAJ
//Sorted list is: RAVI
//SURAJ
//VINIT