如何在 c 中编写一个函数 char*,使 returns 个单词按长度排序?

how to write a function char* in c that returns words sorted in order of their length?

写一个函数,接受一个字符串作为参数,returns它的单词先按长度顺序排序,然后按字母顺序排列,行中用'^'分隔 here is examples of output 字符串中将只有 space、制表符和字母数字字符。

相同大小的单词之间只有一个 space,否则为 ^。

单词是由 ​​spaces/tabs 或字符串的 start/end 分隔的字符串的一部分。如果一个单词只有一个字母,则必须大写。

字母是集合[a-zA-Z]中的一个字符

这是我的代码,但是 returns 我认为最后一个函数没有问题....

#include <unistd.h>
#include <stdlib.h>

int is_upper(char c)
{
    return c >= 'A' && c <= 'Z';
}

int my_lower(char c)
{
    if (is_upper(c))
        return c + 32;
    return c;
}

int my_strlen(char *s)
{
    int i = 0;
    for (; s[i]; i++)
        ;
    return i;
}

int my_is(char c)
{
    return c == ' ' || c == '\t';
}

char *my_strsub(char *s, int start, int end)
{
    char *res = malloc(end - start);
    int i = 0;
    while (start < end)
        res[i++] = s[start++];
    res[i] = 0;
    return res;
}

int cmp_alpha(char *a, char *b)
{
    while (*a && *b && *a == *b)
    {
         a++;
         b++;
    }
    return my_lower(*a) <= my_lower(*b);
}

int cmp_len(char *a, char *b)
{
     return my_strlen(a) <= my_strlen(b);
}

void my_sort(char *arr[], int n, int(*cmp)(char*, char*))
{
    char *tmp;
    for (int i = 0; i < n; i++)
         for (int j = 0; j < n - 1; j++)
         {
             if ((*cmp)(arr[j], arr[j + 1]) == 0)
             {
                  tmp = arr[j];
                  arr[j] = arr[j + 1];
                  arr[j + 1] = tmp;
             }
         }
}

char* long(char *s)
{
     int start = 0, idx = 0;
     char *words[my_strlen(s) / 2 + 1];
     for (int i = 0; s[i]; i++)
     {
         if (!my_is(s[i]) && i > 0 && my_is(s[i - 1]))
              start = i;
         if (my_is(s[i]) && i > 0 && !my_is(s[i - 1]))
              words[idx++] = my_strsub(s, start, i);
         if (!s[i + 1] && !my_is(s[i]))
              words[idx++] = my_strsub(s, start, i + 1);
     }
     my_sort(words, idx, &cmp_alpha);
     my_sort(words, idx, &cmp_len);
     char* res = malloc(100);
     int pushed=0;
     for (int i = 0; i < idx - 1; i++)
     {
           res[pushed]=*words[i];
           if (my_strlen(&res[pushed]) < my_strlen(&res[pushed + 1]))
           {
                res[pushed]=res[94];
           }
           else
           {
                res[pushed]=res[32];
           }
           pushed++;
     }
     res[pushed]='[=11=]';
     return res;
}

int main()
{
     long("Never take a gamble you are not prepared to lose");
     return 0;
}

除了 my_strsub 中的逐一分配错误外,分离和排序单词似乎效果很好。只有这样,您才会将结果字符数组与字符指针数组 e 混淆。 G。使用 res[pushed]=*words[i] 您只将单词的第一个字符写入结果。 ord_alphlong 的最后一个 for 循环可能是:

if (idx)
     for (int i = 0; ; )
     {
           char *word = words[i];
           int lng = my_strlen(word);
           if (100 < pushed+lng+1) exit(1);                 // too long
           for (int i = 0; i < lng; ) res[pushed++] = word[i++];
           if (++i == idx) break;                           // last word
           res[pushed++] = lng < my_strlen(words[i]) ? '^'  // other size
                                                     : ' '; // same size
     }

当然,为了看到函数的结果,您必须以某种方式输出它。