I am trying to do my lab sheet related to string, array and pointer. The problem is to "to sort 5 string words stored in an array of pointers."
I tried a lot and also searched the solution for the problem in various sites. I have been able to sort the strings alphabetically but is always some part of the other string at the end of a string.
like If I entered strings like laptop and comp then the output will be comptop and laptop
The main code is:
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[j-1], &str[j]) > 0)
{
t=str[j];
str[j]=str[j-1];
str[j-1]=t;
}
}
}
printf("\n");
for(i=0;i<5;i++)
{
printf("%s\n",&str[i]);
}
}