Link to home
Start Free TrialLog in
Avatar of mwinfiel
mwinfiel

asked on

sort inputted strings

I am trying to read in up to five strings and print them in alphabetical order. I have no idea what I am doing wrong. Also, the input does not terminate when nothing is entered like I want it to do. My program fails after it prints the unsorted strings! any help would be appreciated  



 /*============================================================
//
// This program reads in up to five strings and sorts them
//
//============================================================
*/

# include <stdio.h>
# include <string.h>

void print_string_array(char * string_array[], int length);
void sort(char* string_array[], int length);
void exchange(char** pStr1, char** pStr2);

    int main (void)
    {
    int length;
    char str1[1000];
    char str2[1000];
    char str3[1000];
    char str4[1000];
    char str5[1000];
    char * string_array[5] = {str1, str2, str3, str4, str5};

       
    printf ("\nPlease enter up to 5 strings\n");
    printf ("Enter a null string to indicate completion.\n");

      printf ("1: ");
      scanf("%999s", str1);
      if(str1 == NULL)
                {
                    length = 0;
                 }
      printf ("2: ");
      scanf("%999s", str2);
               if(str2 == NULL)
               {
                   length = 1;
               }
      printf ("3: ");
      scanf("%999s", str3);
                if(str3 == NULL)
                {
                     length = 2;
                }
      printf ("4: ");
      scanf("%999s", str4);
                if(str4 == NULL)
                {
                     length = 3;
                 }
      printf ("5: ");
      scanf("%999s", str5);
                if(str5 == NULL)
                {
                    length = 4;
                }

         else
         {
             length = 5;
                }


    if(length == 0)
    {
        printf("No strings were entered!");
        return 1;
    }
         printf("length is %d\n", length);

    printf("Before sort\n");

    print_string_array(string_array, length);
    sort(&string_array, length);


    printf("After sort\n");
    print_string_array(string_array, length);
    printf("\nNormal Termination.");
    return 0;
    }

/*============================================================
// This function exchanges two integers.
//============================================================
*/
    void exchange(char** pStr1, char** pStr2)
    {
        char * temp = *pStr1;
        *pStr1 = *pStr2;
        *pStr2 = temp;
    }

/*============================================================
// This function sorts an array of integers
//============================================================
*/
    void sort(char* string_array[], int length)
    {
        int exchange_done;
        int i;
        int j;

        do
        {
            exchange_done = 0;
            for (i = 0; i < length; i++)
            {   j = i + 1;
                if (strcmp(string_array[i], string_array[j])< 0)
                {
                    exchange(&string_array[i], &string_array[i+1]);
                    exchange_done = 1;
                }
            }
        } while (exchange_done);
    }



/*============================================================
// This function prints the string array.
//============================================================
*/

    void print_string_array(char * string_array[], int length)
    {
        int i;
        for(i = 0; i < length; i++)
        {
            printf("%d: %s \n", i+1, string_array[i]);
        }
    }
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi mwinfiel,

The first thing that you need to do is take the definitions for str1, str2, str3, str4, str5, and string_array out of the main function and put them into the globals block.  (Out of any function.)  As you've defined them, they are stack variables that are allocated at run time.  However, your definition of string_array attempts to initialize the array at compile time.  Obviously you can't initialize the address at compile time when the address isn't defined until run time.

That'll get you a good start!


Good Luck!
Kent
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwinfiel
mwinfiel

ASKER

Paul,   I tried that but my program still fails....
Hi mwinfiel,

OK! What actually goes wrong?

Paul
the program  fails (exits) right after the unsorted output is printed.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi mwinfiel,

It may be:

            for (i = 0; i < length; i++)

You are stepping off the end of the array! Since you are comparing array[i] with array[i+1].

Try:

            for (i = 0; i < length-1; i++)

Still checking!

Paul
I figured out one problem I forgot the & in

if (strcmp(&string_array[i], &string_array[j])< 0)

I am still having a problem though... I am supposed to prompt the user to input up to five strings. When the user is finished entering strings... they are supposed to enter a null string which I am assuming is the return. but, my program won't stop input by entering return
Hi mwinfiel,

That works here now (dont forget Kent's point about swap)! It sorts in reverse order but I am sure you'll crack that one!

Good code for a newbie!

Paul
Hi mwinfiel,

That's because of the new-line character at the end of each string.  Change the format string to "%999s\n" and you should be fine.


Kent
hi friend,
 void sort(char* string_array[], int length)
    {
        int exchange_done;
        int i;
        int j;

        do
        {
            exchange_done = 0;
            for (i = 0; i < length; i++)
            {   j = i + 1;
                if (strcmp(string_array[i], string_array[j])< 0)
                {
                    exchange(string_array[i], string_array[i+1]);// currect like this i think it will work
                    exchange_done = 1;
                }
            }
        } while (exchange_done);
    }