Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C: Sorting a char array

Name: Anonymous 2013-04-03 9:26

The way I am trying to go about it is to find the lowest value in the array, concatenate it onto the end of an output string, then change that value to null so it won't be compared in the next loop iteration. I have had little success.


    char input[40], output[40], lowest, buffer[100];
    int i=0, j=0, position;

    printf("Enter a string (1-40 characters): ");
    fgets(input, 40, stdin);
   
    /*converts to lower case and deletes special characters*/
    for(j=0;j<40;j++)
    {  
        if(input[j]<91 && input[j]>64)
            input[j]+=32;
        if(input[j]<97 || input[j]>122)
            input[j]='*';
    }

    j=0;

    for(j=0;j<40;j++)
    { 
        lowest=input[j];
        position=j;
        for(i=0;i<40;i++)
        {
            if(lowest!=NULL);
            {
                if(input[i] < lowest)
                {
                    position=i;
                    lowest=input[i];
                }
            }
        }
        if(lowest!=NULL);
        {
            sprintf(buffer, "%c", cvalue);
            strcat(output, buffer);
            memset(buffer, 0, sizeof(buffer));
            input[cposition]='*';
            cvalue=NULL;
        }
    }

    printf("Output: %s", output);


So far nothing is printed.

Name: Anonymous 2013-04-03 9:27

That's supposed to be NULL, not '*' on line 13.

Name: Anonymous 2013-04-03 10:20



    printf("Output: %s", &output[0]); ?

Name: Anonymous 2013-04-03 10:25

hmm... what about an in-place recursive binary sort? ^^

Name: Anonymous 2013-04-03 10:26

>>4
^____________________________________________________________________________________^;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Name: Anonymous 2013-04-03 10:38

8 7 6 5 4 3 2 1

>4 3 2 1 8 7 6 5 (4<>8 / 7<>3 /...) step 4 x4

>2 1 4 3 6 5 8 7 (2<>4 / 3<>1 /...) step 2 x6

>1 2 3 4 5 6 7 8  (2<>1 / 2<|>4 /...) step 1 x7

Name: Anonymous 2013-04-03 11:17

I managed to sort it. Now I'm having trouble removing special characters from the string.

Name: Anonymous 2013-04-03 12:21

'C' is not < 91, dipshit. use casts

Name: Anonymous 2013-04-03 12:55

>>8
Actually that would make a lot of sense.
Finished btw. Not quickly enough to get some sleep though.

Don't change these.
Name: Email:
Entire Thread Thread List