C中不区分大小写的排序

标签 c sorting case-sensitive case-insensitive

我有一个 .txt 文件,我想使用 C 编程对其进行排序。我有用于排序 .txt 文件的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LEN 100 // Length of each line in input file.

int main(void)
{
    char *strFileName = "/home/milad/Desktop/ddd.txt";
    char *strFileSummary = "/home/milad/Desktop/ddd2.txt";
    char strTempData[MAX_LEN];
    char **strData = NULL; // String List
    int i, j;
    int noOfLines = 0;

    FILE * ptrFileLog = NULL;
    FILE * ptrSummary = NULL;

    if ( (ptrFileLog = fopen(strFileName, "r")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileName);
        return 1;
    }
    if ( (ptrSummary = fopen(strFileSummary, "a")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileSummary);
        return 1;
    }

    // Read and store in a string list.
    while(fgets(strTempData, MAX_LEN, ptrFileLog) != NULL) {
        // Remove the trailing newline character
        if(strchr(strTempData,'\n'))
            strTempData[strlen(strTempData)-1] = '\0';
        strData = (char**)realloc(strData, sizeof(char**)*(noOfLines+1));
        strData[noOfLines] = (char*)calloc(MAX_LEN,sizeof(char));
        strcpy(strData[noOfLines], strTempData);
        noOfLines++;
    }
    // Sort the array.
    for(i= 0; i < (noOfLines - 1); ++i) {
        for(j = 0; j < ( noOfLines - i - 1); ++j) {
            if(strcmp(strData[j], strData[j+1]) > 0) {
                strcpy(strTempData, strData[j]);
                strcpy(strData[j], strData[j+1]);
                strcpy(strData[j+1], strTempData);
            }
        }
    }
    // Write it to outfile. file.
    for(i = 0; i < noOfLines; i++)
        fprintf(ptrSummary,"%s\n",strData[i]);
    // free each string
    for(i = 0; i < noOfLines; i++)
        free(strData[i]);
    // free string list.
    free(strData);
    fclose(ptrFileLog);
    fclose(ptrSummary);
    return 0;
} 

此代码区分大小写,它首先对大写字母进行排序,然后对小写字母进行排序,这不是我想要的。我希望它按字母顺序对字母进行排序,并且对字母的大小写不敏感。我知道 ASCII 码以及为什么会出现此问题,但我找不到解决它的方法。

如何更改代码以使其不区分大小写?

最佳答案

如果您的系统有 POSIX strcasecmp功能可用,将 strcmp 替换为 strcasecmp 以进行直接替换。

否则,在自己的代码中实现不区分大小写的字符串比较,并用它替换strcmp的调用。您可以通过将字符串两端的字符转换为相同的大小写(大写或小写)后逐个字符地比较字符串来实现所需的功能。

注意 1: 您的算法效率低下:您以最大长度分配所有字符串,以避免在字符串长度不等时出现未定义的行为。您可以按精确长度分配字符串并避免未定义的行为,因为您根本不需要复制内容:交换字符串指针即可完成这项工作,而且速度会快得多。

if(strcasecmp(strData[j], strData[j+1]) > 0) {
    char *tmp = strData[j];
    strData[j] = strData[j+1];
    strData[j+1] = tmp;
}

注意 2: 冒泡排序算法非常慢,除非数组一开始就接近排序。对于较大的文件,您可以使用 qsort 显着提高速度。 .

关于C中不区分大小写的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453481/

相关文章:

c++ - C - 打印 float - 转换为 int 时精度损失

c - 在没有额外库的情况下解析 C 中的 PEM key

c - 如何在 C 中将 float 放入 char 数组中?

sql-server - SQL Server 区分大小写的数据库名称

c# - 在 VB.NET 中使用 Dim foo As Foo 有问题吗?

c - 为什么在 C 中逐列复制二维数组比逐行复制花费的时间更长?

swift - tableview 按距离快速排序

java - 从对象数组中删除对象

algorithm - 有没有办法在比 O(n^3) 时间更好的时间内解决 4Sum 问题?

php - CakePHP 2.3 在生产环境中加载模型类,但在测试环境中不加载 "case sensitive cause"