簡體   English   中英

C-按字母順序對字符串排序,而無需strcmp()

[英]C - sorting strings alphabetically without strcmp()

我正在編寫一個小型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;
        }

有人能幫我嗎?

提前致謝。

問題不在於掃描,而在於以下幾行:

                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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM