按字母顺序对C字符串数组进行排序

sgupta

我有一个家庭作业问题,我被要求使用C ++按字母顺序对C字符串数组进行排序,所使用的排序算法必须是冒泡排序。到目前为止,我所做的事情(在下面重复)可以对数组进行排序,但只能基于第一个字母。如何进一步对具有相同初始字母的字符串进行排序?

<snipped>@arch:~/College/OOP/Lab/W3$ cat 2.cpp

/*
 * Write a function which sorts an array of C strings in ascending order using bubble sort. The
 * number of strings in the array and the array must be passed as parameters to the function
 */

#include <iostream>
#include <cstring>

using namespace std;

void sort(char **sar, unsigned num, unsigned len)
{
    char *temp = new char[len];

    if (temp == NULL)
    {
        cout << "\nOut-Of-Memory\n";
        return;
    }

    for (unsigned a = 0; a < num-1; a++)
    {
        for (unsigned b = 0; b < ((num-a)-1); b++)
        {
            if (sar[b][0] > sar[b+1][0])
            {
                strcpy(temp, sar[b]);
                strcpy(sar[b], sar[b+1]);
                strcpy(sar[b+1], temp);
            }
        }
    }

    delete[] temp;
}

int main(int argc, char *argv[])
{
    char **sar;
    unsigned num;
    unsigned len;

    cout << "Number of Strings: ";
    cin  >> num;
    cout << "Length of Strings: ";
    cin  >> len;

    cin.ignore(); // Flush buffer to fix a bug (getline after cin).

    sar = (char **) new char*[num];
    if (sar == NULL)
    {
        cout << "\nOut-Of-Memory\n";
        return -1;
    }

    for (unsigned i = 0; i < num; i++)
    {
        sar[i] = (char *) new char[len];
        if (sar[i] == NULL)
        {
            // Let's pretend we 'know' memory management
            // because obviously modern OSs are incapable
            // of reclaiming heap from a quitting process..
            for (unsigned j = 0; j < i; j++)
                delete[] sar[j];
            cout << "\nOut-Of-Memory\n";
            return -1;
        }
    }

    for (unsigned x = 0; x < num; x++)
        cin.getline(&sar[x][0], 512);

    sort(sar, num, len);

    cout << '\n';
    for (unsigned y = 0; y < num; y++)
        cout << sar[y] << '\n';

    for (unsigned z = 0; z < num; z++)
        delete[] sar[z];
    delete[] sar;

    return 0;
}
伊利亚·伯索夫(IłyaBursov)

改变

if (sar[b][0] > sar[b+1][0])

if (stricmp(sar[b], sar[b+1]) > 0)

更新:您可以使用strcasecmp代替stricmp

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何按字母顺序对字符串数组进行排序并防止c中的突变

按字母顺序对字符串数组进行排序 C++

通过在c中按字母顺序对2d字符串数组进行排序

按字母顺序对字符串数组进行排序 - C++ 11

按字母顺序,数字顺序和特殊字符对字符串数组进行排序

在字符串中按字母顺序对字符进行排序

如何按字母顺序对字符串的ArrayList进行排序?

如何按字母顺序对字符串进行排序

C#按字母顺序对字符串数组进行排序,注意将以大写字母开头的字符串放在首位。第一

Visual Basic字符串数组按字母顺序排序

如何根据名称和查询字符串按字母顺序对对象数组进行排序?

使用指针按字母顺序对字符串数组进行排序

如何按字母顺序对记录名称为:字符串字段的记录数组进行排序?

如何使用角度orderBy过滤器按字母顺序对字符串数组进行排序?

如何在Java中按字母顺序对字符串数组进行排序?

使用指针数组按字母顺序对字符串进行排序

如何使用基于域的列表按字母顺序对字符串数组进行排序

Ruby:按字母顺序对字符串数组进行排序,其中包括一些字符串数组

尝试使用冒泡排序按字母顺序对字符串数组进行排序,但排序返回数组的反向

如何在C ++中按字母顺序对标准输入字符串进行排序?

C#按字母顺序对字符串进行排序,后跟出现频率

在C中按字母顺序对字符串和结构进行排序

如何按字母顺序对由逗号分隔的字符串进行排序,并按成员的姓氏按字母顺序进行排序?

在C ++中按字典顺序对字符串进行排序

如何在Python中按字母顺序对字符串中的字母进行排序

Python:如何按字母顺序对字符串中的字母进行排序,以区分大写和小写

在PostgreSQL中按字母顺序对字符串中的字母进行排序

按字母顺序对字符串变量中的字母进行排序

如何使用sql按字符串排序的字母顺序对单词进行排序