Thread: Help sorting my array of strings

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Help sorting my array of strings

    I am trying to sort the contents of my array in order but when I compile my code, my program crashes. Anyone see the problem?

    Code:
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    
    void Swap(char *x, char *y);
    void BubbleSort(char *S[], const int size);
    
    struct WordStruct	// Structure Definition
    {
    	char Word[51];	// A "Word" is a string that can store up to 50 characters and the null string terminator
    	int length;		// Number of characters stored in the Word (not including null)
    } WordList[25];		// Declare an array to store 25 words
    
    int main()
    {
        int i=0;
        char *wordtok;
        char InputString[] = "the cat in the hat jumped over the lazy fox done";
    
        wordtok = strtok( InputString, " ");
    
        while(wordtok != NULL)
        {
            strcpy(WordList[i].Word, wordtok);
            printf("%s ", WordList[i].Word);
            wordtok = strtok( NULL, " ");
            i++;
        }
    
        BubbleSort(WordList, i);
    
        return 0;
    }
    
    void BubbleSort(char *S[], const int size)
    {
       int pass;
       int j;
       char *tempPtr;
    
       for (pass=0; pass < size - 1; pass++)
       {
          for (j=0; j<size - 1; j++)
          {
             if (strcmp(S[j], S[j + 1]) > 0)
             {
                /* swap pointers */
                tempPtr = S[j];
                S[j] = S[j+1];
                S[j+1] = tempPtr;
             }
          }
       }
    
       for (j=0; j<=size; j++)
       {
           printf("%s\n", S[j]);
       }
    }
    
    void Swap(char *x, char *y)
    {
      char temp;
    
      temp = *x;
      *x = *y;
      *y = temp;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're passing something which isn't a char array to your sort function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    How can I sort my wordlist then?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do you want to sort an array of strings, or an array of WordLists? Your function takes an array of strings, but look what you're passing in.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    I want to sort an array of WordLists, what is the proper code to pass the WordList as a parameter to my function? Thanks for the help guys.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    void Swap(struct WordStruct *x, struct WordStruct*y);
    void BubbleSort(struct WordStruct *S, const int size);
    Make the definitions agree with these.

    Make the code inside the function use S[i].member
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Code:
    void BubbleSort(struct WordStruct *S, const int size)
    Is this how it should look for my function? It won't compile.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you change your prototype to match? Did you change your call to BubbleSort to match? It would help greatly if you gave us error messages.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    How should my call be to BubbleSort now?

    I'm new to this forum and C programming, experienced in VB6 though and sorry I should have given you error messages... here is my code now:

    Code:
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    
    void Swap(struct WordStruct *x, struct WordStruct*y);
    void BubbleSort(struct WordStruct *S, const int size);
    
    struct WordStruct	// Structure Definition
    {
    	char Word[51];	// A "Word" is a string that can store up to 50 characters and the null string terminator
    	int length;		// Number of characters stored in the Word (not including null)
    } WordList[25];		// Declare an array to store 25 words
    
    int main()
    {
        int i=0;
        char *wordtok;
        char InputString[] = "the cat in the hat jumped over the lazy fox done";
        char *array[25];
    
        wordtok = strtok( InputString, " ");
    
        while(wordtok != NULL)
        {
            strcpy(WordList[i].Word, wordtok);
            printf("%s ", WordList[i].Word);
            wordtok = strtok( NULL, " ");
            i++;
        }
    
        BubbleSort(WordList, i);
    
        return 0;
    }
    
    void BubbleSort(struct WordStruct *S, const int size)
    {
       int pass;
       int j;
       char *tempPtr;
    
       for (pass=0; pass < size - 1; pass++)
       {
          for (j=0; j<size - 1; j++)
          {
             if (strcmp(S[j].Word, S[j + 1].Word) > 0)
             {
                /* swap pointers */
                tempPtr = S[j].Word;
                S[j].Word = S[j+1].Word;
                S[j+1].Word = tempPtr;
             }
          }
       }
    
       for (j=0; j<=size; j++)
       {
           printf("%s\n", S[j].Word);
       }
    }
    
    void Swap(struct WordStruct *x, struct WordStruct *y)
    {
      char temp;
    
      temp = *x.Word;
      *x.Word = *y.Word;
      *y.Word = temp;
    }
    Error messages:

    C:\Users\Joe\Desktop\alg5.c|50|error: incompatible types when assigning to type 'char[51]' from type 'char *'|
    C:\Users\Joe\Desktop\alg5.c|36|error: conflicting types for 'BubbleSort'|
    C:\Users\Joe\Desktop\alg5.c|62|error: conflicting types for 'Swap'|
    C:\Users\Joe\Desktop\alg5.c|51|error: incompatible types when assigning to type 'char[51]' from type 'char *'|

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Then in the bubble sort you swap the entire structure:
    Code:
    struct Wordstruct temp;
    ...
    temp = x[j];
    x[j] = x[j+1];
    x[j+1] = temp;

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Don't think that would fix my problem as I'm not even using my swap function yet. How should I call BubbleSort now that I changed the parameters?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, I got a bunch more errors, so you should probably turn the warnings all the way up on your compiler:
    Code:
    $ gcc -Wall sort.c
    sort.c:5: warning: ‘struct WordStruct’ declared inside parameter list
    sort.c:5: warning: its scope is only this definition or declaration, which is probably not what you want
    sort.c:6: warning: ‘struct WordStruct’ declared inside parameter list
    sort.c: In function ‘main’:
    sort.c:21: warning: implicit declaration of function ‘strtok’
    sort.c:21: warning: assignment makes pointer from integer without a cast
    sort.c:25: warning: implicit declaration of function ‘strcpy’
    sort.c:25: warning: incompatible implicit declaration of built-in function ‘strcpy’
    sort.c:27: warning: assignment makes pointer from integer without a cast
    sort.c:31: warning: passing argument 1 of ‘BubbleSort’ from incompatible pointer type
    sort.c:6: note: expected ‘struct WordStruct *’ but argument is of type ‘struct WordStruct *’
    sort.c:19: warning: unused variable ‘array’
    sort.c: At top level:
    sort.c:36: error: conflicting types for ‘BubbleSort’
    sort.c:6: note: previous declaration of ‘BubbleSort’ was here
    sort.c: In function ‘BubbleSort’:
    sort.c:46: warning: implicit declaration of function ‘strcmp’
    sort.c:50: error: incompatible types when assigning to type ‘char[51]’ from type ‘char *’
    sort.c:51: error: incompatible types when assigning to type ‘char[51]’ from type ‘char *’
    sort.c: At top level:
    sort.c:62: error: conflicting types for ‘Swap’
    sort.c:5: note: previous declaration of ‘Swap’ was here
    sort.c: In function ‘Swap’:
    sort.c:66: error: request for member ‘Word’ in something not a structure or union
    sort.c:67: error: request for member ‘Word’ in something not a structure or union
    sort.c:67: error: request for member ‘Word’ in something not a structure or union
    sort.c:68: error: request for member ‘Word’ in something not a structure or union
    1. You need to move your struct definition above the prototypes so the compilers knows what WordStruct is before it sees it in the prototypes.
    2. The correct header file is string.h (singular, not plural).
    3. You can't swap arrays of chars like you do. You would need a strcpy, but since you have to swap the entire structure, make temp a struct WordStruct type and just assign it like temp = S[i].

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I was not considering your swap function since you never call it. I was commenting on how to swap structures inside BubbleSort(). I removed Swap() from my source code so that I didn't have to deal with its problems.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Works perfectly now. Here is the code if anyone would like to see and a MAJOR thank you to everyone here. Expect to see me here more often either asking for help or trying my best to help others who need help.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct WordStruct	// Structure Definition
    {
    	char Word[51];	// A "Word" is a string that can store up to 50 characters and the null string terminator
    	int length;		// Number of characters stored in the Word (not including null)
    } WordList[25];		// Declare an array to store 25 words
    
    void BubbleSort(struct WordStruct *S, const int size);
    
    int main()
    {
        int i=0;
        char *wordtok;
        char InputString[] = "the cat in the hat jumped over the lazy fox done";
        char *array[25];
    
        wordtok = strtok( InputString, " ");
    
        while(wordtok != NULL)
        {
            strcpy(WordList[i].Word, wordtok);
            printf("%s ", WordList[i].Word);
            wordtok = strtok( NULL, " ");
            i++;
        }
    
        BubbleSort(WordList, i);
    
        return 0;
    }
    
    void BubbleSort(struct WordStruct *S, const int size)
    {
       int pass;
       int j;
       struct WordStruct tempPtr;
    
       for (pass=0; pass < size - 1; pass++)
       {
          for (j=0; j<size - 1; j++)
          {
             if (strcmp(S[j].Word, S[j + 1].Word) > 0)
             {
                /* swap pointers */
                tempPtr = S[j];
                S[j] = S[j+1];
                S[j+1] = tempPtr;
             }
          }
       }
    
       for (j=0; j<=size; j++)
       {
           printf("%s\n", S[j].Word);
       }
    }

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The inner loop of your bubble sort should probably start at j = pass+1, since you know everything below pass is already sorted. Also, when you do this, you will need to make your loop stop at size - 2 to avoid j going out of bounds when you do S[j + 1].

    Also, for clarity/correctness, you should probably drop the Ptr from tempPtr, since it's not a pointer any more. This is why many people dislike Hungarian notation -- systems Hungarian at least. What you did isn't technically Hungarian, but you changed your type and now people get confused as to whether tempPtr is supposed to be a pointer and you screwed up the declaration, or if it's supposed to be a struct and it's misnamed.

    Okay, so my suggestions for your bubble sort algorithm are a little off (hey, it's been 15 years!). The more correct implementation is:
    Code:
    for (pass = 0; pass < size - 1; pass++) {
        for (j = size - 2; j > pass; j--) {
            // check for inverted elements and swap
        }
    }
    Last edited by anduril462; 03-10-2011 at 01:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-02-2011, 07:35 PM
  2. sorting an array of strings
    By porstart in forum C Programming
    Replies: 3
    Last Post: 02-22-2011, 10:43 PM
  3. Sorting array structures
    By Jaxtomtom89 in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 06:09 AM
  4. Sorting an array of strings
    By porstart in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 09:46 PM
  5. Replies: 3
    Last Post: 08-16-2010, 10:00 AM