//program to print the sentences in lexicographical order
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j;
char a[n][20],temp[20];
printf("enter the number of strings");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the string%d",i);
fgets(a[i],sizeof(a[i]),stdin);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
printf("In lexicographical order");
for(i=1;i<=n;i++)
{
fputs(a[i],stdout);
}
return 0;
}
output:
enter the number of strings4
enter the string1enter the string2raja
enter the string3sekar
enter the string4hari
In lexicographical order
hari
raja
sekar
I have included both input and output. In the output screen it is displaying enterthestring1enterthestring2
, I need to get only one sentence enterthestring1
. What's the error buddy