0

The following program is supposed to sort eight strings in alphabetical order, but its not functioning properly. Can anyone please debug this

void main()
{
	char *p[8], *s;
	int i,j,t;
	clrscr();
	printf("Enter the strings:\n");
	for(i=0;i<8;i++)
		gets(p[i]);
	for(i=0;i<8;i++)
	{
	       for(j=i+1;j<8;j++)
	       {
			t=strcmp(p[i],p[j]);
			if(t>0)
			{
				s=p[i];
				p[i]=p[j];
				p[j]=s;
			}
	       }
	}
	printf("\nSorted strings:\n");
	for(i=0;i<8;i++)
		puts(p[i]);
	getch();
}
1
Contributor
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Rupindersingh
0

Please help me. Its a spooky program. It works fine if we initialize the string p where it is declared, but does not work properly if we get the strings by gets() or scanf().

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.