用双指针排序字符串中的字母

时间:2017-11-10 22:42:22

标签: c string sorting pointers letters

--------------------------关闭-------------------- --------

2 个答案:

答案 0 :(得分:0)

不确定为什么需要“a”数组,因为您可以使用新的字符串数组交换字符。另外,使用char *来保存值的长度有点奇怪,但我想这很有效,因为字符串长度非常短。

答案 1 :(得分:0)

不确定是否要对字母或单词进行排序。评论部分对单词进行排序 检查malloc的返回,因为它可能会失败。

#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;
}