I have to write an interactive program using scanf() to read in seven strings input by the user. The program should print the seven strings as a list, then sort them alphabetically and print a new list. I have to use the function strcmp() to assist in the sorting of the strings and also use the preprocessing directive #define N_STRINGS 7. The program must sort a different number of strings by changing only that line. This is what I have so far.
#include stdio.h
#include string.h
#define N_STRINGS 7
#define arrayword 15
#define maxletters 15

int main(void)
{
int i, j, c;
char line[N_STRINGS][arrayword];
char temp[maxletters];

printf("\nPlease type in %d words : ",N_STRINGS);
for (i = 0; i < N_STRINGS; ++i)
scanf("%s", line);

printf("\nHere is the list you entered:\n\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", line);

printf("\n\nalphabetically sorted for you:\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", line);

return 0;
}Error 1 error C2006: '#include' : expected a filename, found 'identifier' c:\documents and settings\skyah\my documents\visual studio 2005\projects\10 10\10 10\10 10.cpp 1
Error 2 fatal error C1083: Cannot open include file: '': No such file or directory c:\documents and settings\skyah\my documents\visual studio 2005\projects\10 10\10 10\10 10.cpp 1
PLEASE HELP ME IF YOU CAN.
Can someone please help me? These are the error messages i received:

#include [B]<[/B]stdio.h[B]>[/B]
#include [B]<[/B]string.h[B]>[/B]
#define N_STRINGS 7
#define arrayword 15
#define maxletters 15

int main(void)
{
   int i, j, c;
   char line[N_STRINGS][arrayword];
   char temp[maxletters];

   printf("\nPlease type in %d words : ",N_STRINGS);
   for ( i = 0; i < N_STRINGS; ++i )
      scanf("%s", line[i]);

   printf("\nHere is the list you entered:\n\n");
   for ( i = 0; i < N_STRINGS; ++i )
      printf("%s\n", line[i]);

   printf("\n\nalphabetically sorted for you:\n");
   for ( i = 0; i < N_STRINGS; ++i )
      printf("%s\n", line[i]);

   return 0;
}

Hi ,

Here's the solution:

#include <stdio.h>
#include <string.h>
#define N_STRINGS 7
#define arrayword 15
#define maxletters 15

void sort_words(char[][15]);

int main(void)
{
    int i;
    char line[N_STRINGS][arrayword];


    printf("\nPlease type in %d words : \n",N_STRINGS);
    for (i = 0; i < N_STRINGS; ++i)
    scanf("%s", line[i]);

    printf("\nHere is the list you entered:\n\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", line[i]);

    sort_words(line);

    printf("\n\nalphabetically sorted for you:\n");
    for (i = 0; i < N_STRINGS; ++i)
    printf("%s\n", line[i]);

    return 0;
}

/* Using Bubble sorting technique to sort words */
void sort_words(char words_array[][15])
{
    int i,j;
    char temp[arrayword];
    for(i=0;i<N_STRINGS-1;i++)
    {
        for(j=i+1;j<N_STRINGS;j++)
        {
            if(strcmp(words_array[i],words_array[j])>0)
            {
                strcpy(temp,words_array[i]);
                strcpy(words_array[i],words_array[j]);
                strcpy(words_array[j],temp);
            }
        }
    }
}

greetings
Parthiban

>Here's the solution:
Great. Now the OP has learned absolutely nothing from this exercise. I hope you end up working with someone who breezed through school without having to learn, because then you'll realize why giving away the entire solution like that hurts more than it could possibly help.

This article has been dead for over six months. Start a new discussion instead.