C String - 按第一个字的长度排序

时间:2014-06-15 05:56:02

标签: c string sorting pointers boolean

我自学C并做一个练习,除其他外,要求我按字符串中第一个单词的长度对用户输入的字符串列表进行排序。练习中的其他功能(包括按整个长度排序字符串)很容易编写。我已经在这个工作了三个多小时,无法让它工作。我将一个指针数组排序为char,然后在main()函数中使用for循环打印它们。

这可能是一种更简单的方法,但即便如此,我也无法理解为什么这个功能不起作用。我已经做了大约三十次更改,排序仍然很随意。

void srtlengthw(char * strings[], int n)
{
int top, seek, ct, ct_temp, i;
int ar_ct[n]
char * temp;
bool inWord;

for (top = 0, ct = 0, i = 0, inWord = false; top < n - 1; top++)
{
        while (strings[top][i])
        {
                if (!isblank(strings[top][i]))
                {
                        i++;
                        ct++;
                        inWord = true;
                }
                else if (!inWord)
                        i++;
                else
                        break;
        }
        ar_ct[top] = ct;
        for (seek = top + 1, ct = 0, i = 0, inWord = false; seek < n; seek++)
        {
                while(strings[seek][i])
                {
                        if (!isblank(strings[seek][i]))
                        {
                                i++;
                                ct++;
                                inWord = true;
                        }
                        else if (!inWord)
                                i++;
                        else
                                break;
                }
                ar_ct[seek] = ct;
                if (ar_ct[top] > ar_ct[seek])
                {
                        ct_temp = ar_ct[top];
                        ar_ct[top] = ar_ct[seek];
                        ar_ct[seek] = ct_temp;
                        temp = strings[top];
                        strings[top] = strings[seek];
                        strings[seek] = temp;
                }
        }
}}

根据要求输出错误的示例:

输入:

玛丽

羊肉

处死

撒但

======

输出:

羊肉

处死

玛丽

撒但

这是一个功能正常的简单功能的例子。它意味着按整个字符串的长度对指针进行排序,而不仅仅是第一个单词。我尝试在这一个上对单词长度排序函数进行建模,但是我很难处理我的计数器变量,也许我的bool标志正确。

void srtlength(char * strings[], int n)
{
        int top, seek;
        char * temp;

        for (top = 0; top < n - 1; top++)
                for (seek = top + 1; seek < n; seek++)
                        if (strlen(strings[top]) > strlen(strings[seek]))
                        {
                               temp = strings[top];
                               strings[top] = strings[seek];
                               strings[seek] = temp;
                        }
}

对克雷格来说,希望这有帮助吗? 输入:

They say it's lonely at the top, and whatever you do
You always gotta watch m*********s around you
Nobody's invincible
No plan is foolproof
We all must meet our moment of truth
The same sheisty cats that you hang with and do your thang with
Could set you up and wet you up, n***a, peep the language
It's universal
You play with fire, it may hurt you, or burn you
Lessons are blessins you should learn through

为我输出:

You always gotta watch m********s around you
Nobody's invincible
No plan is foolproof
We all must meet our moment of truth
The same sheisty cats that you hang with and do your thang with
Could set you up and wet you up, n***a, peep the language
It's universal
You play with fire, it may hurt you, or burn you
Lessons are blessins you should learn through
They say it's lonely at the top, and whatever you do

2 个答案:

答案 0 :(得分:1)

如果您正在寻找类似于您发布的示例代码的输出,那么我建议将其用作具有您预期行为的版本的模板。我想指出的关键是它按strlen函数的返回值排序。

strlen是C的<string.h>库中的函数(我认为?),它返回C风格字符串的长度。在C中,正如您可能知道的那样,字符串的结尾由空终止符标识,表示为'\0'

虽然从一个库到另一个库的精确strlen可能不同,但这是一个标准实现(更容易阅读):

int strlen(char * str){
    char * l;
    for(l = str; *l != '\0'; l++);
    return l - str;
}

人们可能会争辩说这有问题并且它并不完美,但它确实显示了如何确定字符串的长度。

现在我们知道最后一个示例按总字符串长度排序,并且我们知道如何确定字符串长度,我们可能会使我们自己的strlen版本在第一个单词后停止,而不是停止在null终止符:

int blank_strlen(char * str){
    char * l;
    for(l = str; *l != '\0' && !isblank(*l); l++);
    return l - str;
}

现在,使用给出的示例代码:

void blank_srtlength(char * strings[], int n)
{
        int top, seek;
        char * temp;

        for (top = 0; top < n - 1; top++)
                for (seek = top + 1; seek < n; seek++)
                        if (blank_strlen(strings[top]) > blank_strlen(strings[seek]))
                        {
                               temp = strings[top];
                               strings[top] = strings[seek];
                               strings[seek] = temp;
                        }
}

答案 1 :(得分:1)

millinon的答案是一个更好的方法,因为它更简单。但是,如果您正在寻找代码无法工作的原因,那是因为您的变量仅在每个循环之外重置。 这段代码:

    for (seek = top + 1, ct = 0, i = 0, inWord = false; seek < n; seek++)
    {
            while(strings[seek][i])
在首次启动循环之前,

仅设置ct,i和inWord 一次。当程序循环时,ct,i和inWord的值将保留在最后一次迭代中。

在循环中移动赋值如下:

    for (seek = top + 1; seek < n; seek++)
    {
            ct = 0;
            i = 0;
            inWord = false;
            while(strings[seek][i])

将解决您的问题(您必须在两个地方都这样做。)

相关问题