【问题标题】:C - sorting strings alphabetically without strcmp()C - 在没有 strcmp() 的情况下按字母顺序排序字符串
【发布时间】:2014-03-23 10:59:04
【问题描述】:

我正在编写一个小型 C 程序,它应该按字母顺序对逗号分隔的字符串进行排序。

输入如下所示:"FSKT","EORD","OSEA","DA","ERTS","VG","FHR","EIAS","DOD"

这是进行排序的工作代码:

#include <stdio.h>
#include <string.h> 
int main() {
    char *a[] = {"FSKT","EORD","OSEA","DA","ERTS","VG","FHR","EIAS","DOD"};
    const char *s1,  *s2;
    int compRes,i,j;
    int length = 9;
    for (i = 0; i < length; i++) {
        for (j = i+1; j < length; j++){
            s1 = a[i];
            s2 = a[j];
            compRes = 0;
            while(*s1 && *s2){              
                if(*s1!=*s2){               
                    compRes = *s1 - *s2;     
                    break;                   
                }                           
                ++s1;
                ++s2;
            }
            if (compRes > 0 || (compRes == 0 && *s1)) {
                char* temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        printf("%s ", a[i]);

    }
    printf("\n");
    return 0;
}

我没有使用 strcmp() 或类似的东西,因为它需要保持非常基本以供以后翻译。 现在我想使用 scanf() 作为输入字符串,如果到达一个点,输入应该停止。不知怎的,我已经卡在输入上了……

这是我目前的尝试,不幸的是它不起作用。:

 #include <stdio.h>
    #include <string.h> 
    int main() {

        int compRes;
        int i;
        int j;
        int length = 9;
        char *s1;
        char*s2;
        char a[10][10] = { 0,0,0,0,0,0,0,0,0,0};

        //scan all strings separated with a comma

        scanf("%4[^,],%4[^,],%4[^,],%4[^,],%4[^,],%4[^,],%4[^,],%4[^,],%4[^,]" ,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);



            for (i = 0; i < length; i++) {
                for (j = i+1; j < length; j++){
                    s1 = a[i];
                    s2 = a[j];
                    compRes = 0;
                    while(*s1 && *s2){
                        if(*s1!=*s2){
                            compRes = *s1 - *s2;
                            break;
                        }
                        ++s1;
                        ++s2;
                    }
                    if (compRes > 0 || (compRes == 0 && *s1)) {
                        char* temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }

                }
                printf("%s ", a[i]);

            }
            printf("\n");
            return 0;
        }

有人可以帮我吗?

提前致谢。

【问题讨论】:

  • 你能解释一下不使用strcmp的动机吗
  • 程序应该稍后翻译成汇编语言。
  • 为什么不创建自己的 mystrcmp() 函数并使用它呢?

标签: c sorting strcmp


【解决方案1】:

问题不在于扫描,而在于以下几行:

                char* temp = a[i];
                a[i] = a[j];
                a[j] = temp;

前面的代码适用于指针数组,但不适用于二维字符数组。如果它甚至可以编译,我会感到惊讶。您基本上有两种选择如何修复您的程序。

您可以将字符串扫描到一个二维数组中,并设置正确的指针指向您原来的一维指针数组,并且程序和以前一样。即:

    char b[10][10] = { 0,0,0,0,0,0,0,0,0,0};
    char *a[10];
    for(i=0; i<length; i++) a[i] = b[i];

或者您在进行交换时需要复制整个字符串。即:

               char temp[10];
               strcpy(temp, a[i]);
               strcpy(a[i], a[j]);
               strcpy(a[j], temp);

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2020-02-25
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多