(C++) CPP Programs - 24
Friday, December 14, 2007
24. Write a C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the given names and the sorted names with suitable headings.
/* Alphabetical order*/
#include
#include
#include
void main()
{
char names[45][43],temp[78],names1[45][56];
int i,n,j;
clrscr();
printf("\nEnter how many names\n");
scanf("%d",&n);
printf("\nEnter the names one dy one\n");
for(i=0;i < n;i++)
scanf("%s",names[i]);
for(i=0;i < n-1;i++)
for(j=0;j < n-1-i;j++)
{
if (strcmp(names[j],names[j+1])>0)
{
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}
printf("\nSorted names\n");
for(i=0;i < n;i++)
printf("%s\n",names[i]);
getch();
}
0 comments:
Post a Comment