We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

sort inputted strings

mwinfiel
mwinfiel asked
on
Medium Priority
291 Views
Last Modified: 2010-04-15
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]);
        }
    }
Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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
Your problem is here:

    sort(&string_array, length);

You are passing the address of the address of the array! You should just pass the array, which is a pointer in itself.

try changing it to:

    sort(string_array, length);
 
Paul

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

OK! What actually goes wrong?

Paul

Author

Commented:
the program  fails (exits) right after the unsorted output is printed.
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
Hi mwinfiel,

Look in the sort routine.  Two problems with it.  1)  It won't really sort.  2)  You're testing the values of string_array at *i* and *j* but passing the values at *i* and *i+1* to swap().  You're over-indexing the array.


Kent
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

Author

Commented:
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
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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);
    }

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.