Write a program that uses an array of pointers. Elements of the array point to strings. Now reorder the pointers so that they point to the strings in sorted order.
main()
{
char *str[10], *t;
int i,j;
for(i=0;i<5;i++)
{
scanf("%s",&str[i]);
}
for(i=0; i<5; i++)
{
for(j=i+1; j<5; j++)
{
if (strcmp(&str[i], &str[j]) > 0)
{
t=str[j];
str[j]=str[i];
str[i]=t;
}
}
}
printf("\n");
for(i=0;i<5;i++)
{
printf("%s\n",&str[i]);
}
}
main()
{
char *str[10], *t;
int i,j;
for(i=0;i<5;i++)
{
scanf("%s",&str[i]);
}
for(i=0; i<5; i++)
{
for(j=i+1; j<5; j++)
{
if (strcmp(&str[i], &str[j]) > 0)
{
t=str[j];
str[j]=str[i];
str[i]=t;
}
}
}
printf("\n");
for(i=0;i<5;i++)
{
printf("%s\n",&str[i]);
}
}
No comments:
Post a Comment