1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I sort an array of cstrings?

Discussion in 'C' started by jj_evans, Jul 30, 2010.

  1. jj_evans

    jj_evans New Member

    Joined:
    Jun 2, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I need to sort an array of cstrings in ascending order. the cstrings are names and i have to list them all in alphabetical order. The problem is I don't know how to determing which cstring comes first. Below is my code for sorting integers please let me know what to modify to sort cstrings in as much detail as you can. Thank you!

    Code:
    void swap(int& a, int& b)
    {
        int temp;
        temp=a;
        a=b;
        b=temp;
    }
    
    int indexmin(int a[], int start, int arraysize)
    {
        int min = a[start], indexofmin=start;
        
        for(int b=start+1; b<arraysize; b++)
            if(a[b] < min)
            {
                min = a[b];
                indexofmin = b;
                }
                return indexofmin;
    }
    void sort(int array[], int arraysize)
    {
        int indexofnextsmallest;
        
        for(int x=0; x<arraysize; x++){
            indexofnextsmallest = indexmin(array, x, arraysize);
            swap(array[x], array[indexofnextsmallest]);
            }
            }
    
    int search(int array[], int arraysize, int target)
    {
        int index=0;
        bool found = false;
        while (!found && index<arraysize){
        
            if(target==array[index])
                found=true;
            else
            index++;
            }
        if(found)
        
               return index;
        else
         
            return -1;
        }
     
    Last edited by a moderator: Jul 31, 2010
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    like this

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
     
    void swap(char **str1_ptr, char **str2_ptr)
    {
      char *temp = *str1_ptr;
      *str1_ptr = *str2_ptr;
      *str2_ptr = temp;
    }
    
    int indexmin(char **a, int start, int arraysize){
        char *min = a[start];
        int indexofmin=start;
        for(int b=start+1; b<arraysize; b++)
        if(strcmp(a[b] ,min)<0){
            min = a[b];
            indexofmin = b;
        }
        return indexofmin;
    }
    
    void sort(char **array, int arraysize){
        int indexofnextsmallest;
        for(int x=0; x<arraysize; x++){
            
            indexofnextsmallest = indexmin(array, x, arraysize);
            
            swap(&array[x], &array[indexofnextsmallest]);
        }
    }
    
    int search(char **array, int arraysize, char *target){
        int index=0;
        bool found = false;
        while (!found && index<arraysize){
        if(strcmp(target,array[index])==0)
            found=true;
        else
            index++;
        }
        if(found)
            return index;
        else
            return -1;
    } 
    
    int main(){
    char **a=(char **) malloc(4*sizeof(char *));
    for (int i=0;i<4;i++)
        a[i]=(char *) malloc(50*sizeof(char));
    
    a[0]="xxxxxxx";
    a[1]="aaaaa";
    a[2]="cccc";
    a[3]="bbbbbbbbb";
    printf("\nbefore sort....\n");
    for (int i=0;i<4;i++)
         printf("%s\n",a[i]);
    sort(a,4);
    printf("\nafter sort....\n");
    for (int i=0;i<4;i++)
         printf("%s\n",a[i]);
        getchar();
    }
    
    
     
  3. jj_evans

    jj_evans New Member

    Joined:
    Jun 2, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    virxen, thank you very much for your reply! It was extremely helpful. The only thing I don't get is isn't a c-string a multi-dimensional array? if so, shouldnt we use the two brackets when doing the prototype as in void sort(char array[][SIZE]?

    btw, I only use the include <iostream> & include <fstream> & using namespace std;
     
  4. Ancient Dragon

    Ancient Dragon New Member

    Joined:
    Jul 23, 2010
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    part time cashier at WalMart
    Location:
    near St Louis, IL, USA
    >>I need to sort an array of cstrings

    Are you talking abot the CString c++ class that's in Microsoft's MFC library? Or are you talking about something else? (There is no such thing as standard c or C++ called cstring).
     

Share This Page