>I was assuming the user entering a string though could enter more than one word at a
>time & the string would end only when the user hits enter ??
That's not how scanf works. Parsing only goes to the next whitespace with the %s specifier. That means that words will be split up. Fortunately, this is easy to fix, but unfortunately, it's difficult to get right.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N_STRINGS 7
int cmp(const void *a, const void *b);
int main(void)
{
/* Declare an array of strings */
char strings[N_STRINGS][1024];
int i;
/* Read N_STRINGS strings */
for (i = 0; i < N_STRINGS; i++) {
/* A prompt is always helpful to users */
printf("Enter string %%%d: ", i + 1);
/* Unless you print a '\n' in the prompt, you need this */
fflush(stdout);
/* ALWAYS check scanf for success */
/* The [^\n] specifier says to read until '\n' is found */
if (scanf("%1024[^\n]", strings[i]) != 1) {
fprintf(stderr, "Error reading input\n");
return EXIT_FAILURE;
}
/* scanf leaves '\n' in the stream, so we have to remove it */
getchar();
}
/* Sort the array using cmp as the comparison routine */
qsort(strings, N_STRINGS, sizeof strings[0], cmp);
/* Print out the sorted result */
for (i = 0; i < N_STRINGS; i++) {
printf("%s\n", strings[i]);
}
return EXIT_SUCCESS;
}
int cmp(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}
Even that's not perfect, but it'll be more than enough for your purposes. In general, it's a bad idea to use scanf for string data. fgets is better suited to that task.