-2

I'm stuck with sorting letters in a string. It must be sorted in alphabetical order using double pointers.

#define SIZE 21 //defined index of the array

int _tmain(int argc, _TCHAR* argv[])
{
    // an array with 21 strings
    char * string[SIZE] = { "dfghgfd", "rtyukljgfds", "sdsf", "fdgdfhg", "fgfhgjghj", "nmjlkjlk", "qwasazx", 
                                         "zxdfd", "opiljkg", "vcxdfgfd", "fgfhgfhgh", "bvvh", "bb", "dfsdretr", 
                                         "reuio", "cvbmhg", "fgfdyrtyty", "fgdgdfgdfgdf", "g", "fgdfg", "ghghgfhv" };

-----------------------Access to each string in array ------------------------

    int Anz, i; //Anz - 21 strings

    //declared new array
    char** new_string;
    new_string = (char**)malloc(sizeof(string));
    Anz = sizeof(string) / sizeof(char*);

    for (i = 0; i < Anz; i++)
    {
        new_string[i] = (char*)malloc(strlen(string[i]) + 1);
        strcpy(new_string[i], string[i]);
    }
        
    

----------------------- sorting letters--------------------------------------

    char* temp;
    int k, j;

    for (k = 0; k<Anz - 1; k++)
    {
        for (j = k + 1; j<Anz; j++)
        {
            if (new_string[k] > new_string[j])
            {
                temp = new_string[k];
                new_string[k] = new_string[j];
                new_string[j] = temp;
            }
        }
    }

    return 0;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
arsy
  • 25
  • 1
  • 5

2 Answers2

0

Not sure why you need the "a" array, since you can swap the characters using the new string array. Also, using a char * to hold the length of values is sort weird but I guess it works since the string lengths are pretty short.

0

Not sure if you wanted to sort the letters or the words. A commented section sorts the words.
Check the return of malloc as it can fail.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 21 //defined index of the array

int main(int argc, char** argv)
{
    // an array with 21 strings
    char * string[SIZE] = { "dfghgfd", "rtyukljgfds", "sdsf", "fdgdfhg", "fgfhgjghj", "nmjlkjlk", "qwasazx",
                         "zxdfd", "opiljkg", "vcxdfgfd", "fgfhgfhgh", "bvvh", "bb", "dfsdretr",
                         "reuio", "cvbmhg", "fgfdyrtyty", "fgdgdfgdfgdf", "g", "fgdfg", "ghghgfhv" };

    int Anz, i; //Anz - 21 strings
    int width = 0, len = 0;

    //declared new array
    char** new_string;
    Anz = sizeof(string) / sizeof(*string);
    if ( NULL == ( new_string = malloc ( Anz * sizeof( *new_string)))) {
        fprintf ( stderr, "malloc failed\n");
        return 0;
    }

    for (i = 0; i < Anz; i++)
    {
        len = strlen ( string[i]) + 1;
        if ( len > width) {
            width = len;//used later when printing
        }
        if ( NULL == ( new_string [i] = malloc ( width))) {
            fprintf ( stderr, "[i] malloc failed\n");
            //free memory allocated
            while ( i) {
                i--;
                free ( new_string[i]);
            }
            free ( new_string);
            return 0;
        }
        strcpy(new_string[i], string[i]);
    }
/*
    //sort words
    int word = 0;
    while ( word < Anz - 1) {
        int end = word;
        int temp = end + 1;
        while ( end >= 0 && 0 > strcmp ( new_string[temp], new_string[end])) {
            char *hold = new_string[temp];
            new_string[temp] = new_string[end];
            new_string[end] = hold;
            end--;
            temp--;
        }
        word++;
    }
    word = 0;
    while ( word < Anz) {
        printf ( "Anz[%2d] is %s\n", word, new_string[word]);
        word++;
    }
*/
    //sort letters in word
    char swap;
    int sorted;
    int prior;
    int each;
    int start;

    word = 0;
    while ( word < Anz)
    {
        start = 0;//new_string[Anz][0]
        sorted = start;
        prior = start;
        each = start + 1;//new_string[Anz][1]
        printf ( "Anz[%2d] is %-*s", word, width, new_string[word]);
        while ( '\0' != new_string[word][each]) {
            while ( prior >= 0 && new_string[word][each] < new_string[word][prior]) {
                swap = new_string[word][each];
                new_string[word][each] = new_string[word][prior];
                new_string[word][prior] = swap;
                each--;//move toward start of string
                prior--;
            }
            sorted++;//move toward end of string
            prior = sorted;
            each = prior + 1;
        }
        printf ( " sorted %s\n", new_string[word]);
        word++;
    }
    //release allocated memory
    word = 0;
    while ( word < Anz) {
        free ( new_string[word]);
        word++;
    }
    free ( new_string);

    return 0;
}
xing
  • 2,125
  • 2
  • 14
  • 10