Thread: Sorting strings in C

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Sorting strings in C

    So I got the sorting program to work when I set the array without any numbers. However when reading from a file which has numbers before the word it is not behaving the way I want it to.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
      
    
    
    int cmpstr(void* v1, void* v2) 
    { 
         
        char *a1 = *(char**)v1; 
        char *a2 = *(char**)v2; 
        return strcmp(a1, a2); 
    } 
      
    // function for comparing two strings 
    
    
    void swap(void* v1, void* v2, int size) 
    { 
        
        char buffer[size]; 
      
        
        memcpy(buffer, v1, size); 
        memcpy(v1, v2, size); 
        memcpy(v2, buffer, size); 
    } 
      
    
    
    void _qsort(void* v, int size, int left, int right, 
                          int (*comp)(void*, void*)) 
    { 
        void *vt, *v3; 
        int i, last, mid = (left + right) / 2; 
        if (left >= right) 
            return; 
      
        // casting void* to char* so that operations  
        // can be done. 
        void* vl = (char*)(v + (left * size)); 
        void* vr = (char*)(v + (mid * size)); 
        swap(vl, vr, size); 
        last = left; 
        for (i = left + 1; i <= right; i++) { 
      
            
            vt = (char*)(v + (i * size)); 
            if ((*comp)(vl, vt) > 0) { 
                ++last; 
                v3 = (char*)(v + (last * size)); 
                swap(vt, v3, size); 
            } 
        } 
        v3 = (char*)(v + (last * size)); 
        swap(vl, v3, size); 
        _qsort(v, size, left, last - 1, comp); 
        _qsort(v, size, last + 1, right, comp); 
    } 
      
    int main() 
    { 
         
        char* a[] = {"bbc", "xcd", "ede", "def", 
                "afg", "hello", "hmmm", "okay", "how", "welcome" }; 
      
         
        _qsort(a, sizeof(char*), 0, 8, (int (*)(void*, void*))(cmpstr)); 
        
        for (int i = 0; i < 10; i++) 
            printf("%s ", a[i]); 
        printf("\n"); 
      
        
    }
    The desired output for the input is shown.
    Sorting strings in C-screenshot-201-png
    Sorting strings in C-screenshot-202-png
    Last edited by Timmy10; 12-18-2020 at 08:38 AM.

  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
    Well yeah, you need to show how you're reading from the file, how you're parsing the data and how you're constructing the array to be sorted.
    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
    Nov 2020
    Posts
    24

    Made some changes

    @salem I made a few tweaks. Now it comes to segmentation fault.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
      
     
    int cmpstr(void* v1, void* v2) 
    {  
        char *a1 = *(char**)v1; 
        char *a2 = *(char**)v2; 
        return strcmp(a1, a2); 
    } 
      
    
      
    
    void swap(void* v1, void* v2, int size) 
    { 
        
        char buffer[size]; 
      
        
        memcpy(buffer, v1, size); 
        memcpy(v1, v2, size); 
        memcpy(v2, buffer, size); 
    } 
      
    
    void char_sort(void* v, char size, char left, char right, 
                          char (*comp)(void*, void*)) 
    { 
        void *vt, *v3; 
        int i, last, mid = (left + right) / 2; 
        if (left >= right) 
            return; 
      
        
        void* vl = (char*)(v + (left * size)); 
        void* vr = (char*)(v + (mid * size)); 
        swap(vl, vr, size); 
        last = left; 
        for (i = left + 1; i <= right; i++) { 
      
            
            vt = (char*)(v + (i * size)); 
            if ((*comp)(vl, vt) > 0) { 
                ++last; 
                v3 = (char*)(v + (last * size)); 
                swap(vt, v3, size); 
            } 
        } 
        v3 = (char*)(v + (last * size)); 
        swap(vl, v3, size); 
        char_sort(v, size, left, last - 1, comp); 
        char_sort(v, size, last + 1, right, comp); 
    } 
      
    int main() 
    { 
        char a[10];
        FILE *in, *out;
        in = fopen("alphabetic_sort.dat", "r");
        out = fopen("sort.out", "w");
        
        while (1)
        {
            fscanf(in, "%d", &a);
            char_sort(a, sizeof(char*), 0, 10, (char (*)(void*, void*))(cmpstr)); 
        
            int i;
            for (i = 0; i < 9; i++) 
            fprintf(out, "%s \n", a[i]); 
             
        }
        
        
      
      return 0;  
    } 
    

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post it again, this time without the awful fonts from your IDE.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Sorry @salem. Posting it again

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    @salem

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
      
     
    int cmpstr(void* v1, void* v2) 
    {  
        char *a1 = *(char**)v1; 
        char *a2 = *(char**)v2; 
        return strcmp(a1, a2); 
    } 
      
    
    
      
    
    
    void swap(void* v1, void* v2, int size) 
    { 
        
        char buffer[size]; 
      
        
        memcpy(buffer, v1, size); 
        memcpy(v1, v2, size); 
        memcpy(v2, buffer, size); 
    } 
      
    
    
    void char_sort(void* v, char size, char left, char right, 
                          char (*comp)(void*, void*)) 
    { 
        void *vt, *v3; 
        int i, last, mid = (left + right) / 2; 
        if (left >= right) 
            return; 
      
        
        void* vl = (char*)(v + (left * size)); 
        void* vr = (char*)(v + (mid * size)); 
        swap(vl, vr, size); 
        last = left; 
        for (i = left + 1; i <= right; i++) { 
      
            
            vt = (char*)(v + (i * size)); 
            if ((*comp)(vl, vt) > 0) { 
                ++last; 
                v3 = (char*)(v + (last * size)); 
                swap(vt, v3, size); 
            } 
        } 
        v3 = (char*)(v + (last * size)); 
        swap(vl, v3, size); 
        char_sort(v, size, left, last - 1, comp); 
        char_sort(v, size, last + 1, right, comp); 
    } 
      
    int main() 
    { 
        char a[10];
        FILE *in, *out;
        in = fopen("alphabetic_sort.dat", "r");
        out = fopen("sort.out", "w");
        
        while (1)
        {
            fscanf(in, "%d", &a);
            char_sort(a, sizeof(char*), 0, 10, (char (*)(void*, void*))(cmpstr)); 
        
            int i;
            for (i = 0; i < 9; i++) 
            fprintf(out, "%s \n", a[i]); 
             
        }
        
        
      
      return 0;  
    }

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Why are you saving the integer inside the char array? Why are you passing "sizeof(char*)" as the size?

    Btw, you don't want to use "char" for the types of size and left/right. "int" would be much better.

    Also btw, the way I see it, the number in front of each line is the amount of characters you should allocate on the heap(malloc/free etc), inside which you'd read the string.
    Devoted my life to programming...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > However when reading from a file which has numbers before the word it is not behaving the way I want it to.
    This would suggest your input should be
    Code:
    int a;
    char b[100];
    fscanf(in, "%d %s", &a, b);
    Also, before you do any file reading at all, make sure this works.
    Code:
        char a[10] = "qawsedrftg";
        char_sort(a, sizeof(char), 0, 10, (char (*)(void*, void*))(cmpstr)); 
        for (i = 0; i < 9; i++)
            printf("%d = %c\n", i, a[i] );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Strings
    By tsmith94 in forum C Programming
    Replies: 0
    Last Post: 11-29-2011, 09:47 PM
  2. sorting strings
    By sid13 in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2011, 06:58 PM
  3. Sorting strings
    By yacek in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 02:07 PM
  4. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  5. Sorting Strings
    By SteelCityKid in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 01:14 PM

Tags for this Thread