Hey this program is running but not giving any result. its about to rearrange few strings alphabetically -
Code:
//REORDERING A LIST OF STRINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reorder (int n, char x[][12]);
void main()
{
int i,n=0;
char x[10][12];
printf(" Enter each string on a seperate line below\n\n");
printf(" Type\'END\' when finished\n\n");
do
{
printf("string %d:", n+1);
scanf("%s", x[n]);
}
while (strcmp( x[n++], "END"));
n--;
reorder(n,x);
printf("\n\nReordered List of Strings:\n") ;
for( i=0;i<n;++i)
printf("\nstring %d: %s", i+1, x[i]) ;
}
void reorder ( int n, char x[][12])
{
char temp[12];
int i, item;
for( item=0; item= n-1; ++ item)
for(i=item+1; i<n; ++i)
if (strcmp(x[item] , x[i])>0)
{
strcpy(temp, x[item]);
strcpy(x[item], x[i]);
strcpy(x[i], temp);
}
return;
}
and explanation of this part will be helpful-
Code:
do
{
printf("string %d:", n+1);
scanf("%s", x[n]);
}
while (strcmp( x[n++], "END"));
n--;
at first n=0( and later it will increase), to me scanf("%s", x[n]); will take only a letter . How can it take entire string or I'm guessing wrong ?
and as far i know , strcmp function accepts two strings as arguments and returns an integer. whats its doing here , specially with END ?