Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
clrscr();
char *str[5],*temp;
int i,j,n;
printf("\nHow many names do you want to sort:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name %d: ",i);
flushall();
gets(str[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;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("\nSorted List : \n");
flushall();
for(i=0;i<n;i++)
puts(str[i]);
getch();
return(0);
}
Output:
How many names do you want to sort:5
Enter the name 0:Google
Enter the name 1:Bing
Enter the name 2:Amazon
Enter the name 3:Facebook
Enter the name 4:Twitter
Sorted List :
Amazon
Bing
Facebook
Google
Twitter
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
clrscr();
char *str[5],*temp;
int i,j,n;
printf("\nHow many names do you want to sort:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name %d: ",i);
flushall();
gets(str[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;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("\nSorted List : \n");
flushall();
for(i=0;i<n;i++)
puts(str[i]);
getch();
return(0);
}
Output:
How many names do you want to sort:5
Enter the name 0:Google
Enter the name 1:Bing
Enter the name 2:Amazon
Enter the name 3:Facebook
Enter the name 4:Twitter
Sorted List :
Amazon
Bing
No comments:
Post a Comment