Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How do I sort an array of cstrings? (http://www.go4expert.com/forums/i-sort-array-cstrings-t22880/)

jj_evans 31Jul2010 04:12

How do I sort an array of cstrings?
 
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;
    }


virxen 31Jul2010 04:48

Re: How do I sort an array of cstrings?
 
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();
}


jj_evans 31Jul2010 08:20

Re: How do I sort an array of cstrings?
 
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;

Ancient Dragon 31Jul2010 09:40

Re: How do I sort an array of cstrings?
 
>>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).


All times are GMT +5.5. The time now is 00:20.