2015-02-10 158 views
0

我通過搜索位和找不到與沒有使用字符串庫的功能,如STRCMP字符的比較,所以希望這個問題不重複。我來自Java,對C語言有點新鮮。我試圖比較兩個字符串(保存在同一個數組中)以按字母順序對它們進行排序,但試圖在沒有C字符串庫函數的情況下這樣做,並且似乎無法弄清楚這一點。任何人都可以將我指向正確的方向嗎?如何比較兩個字符串中的字符以按字母順序排序? (無C串庫函數)

for (i = 0; i < TotalStrings; i++) { /* TotalStrings is the number of strings in the array */ 
if (length[i] < length[i+1]) { /* compares length of both strings, saved in a different array */ 
for (j = 0; j < length[i]; j++) { 
    if (Strings[i][j] > Strings[i+1][j]) { 
     char temp = Strings[i]; 
     Strings[i] = Strings[i+1]; 
     Strings[i+1] = temp; 
     j = length[i]; 
    } 
    } 
} 
if (length[i] > length[i+1]) { /* compares length of both strings, saved in a different array */ 
for (j = 0; j < length[i+1]; j++) { 
    if (Strings[i][j] > Strings[i+1][j]) { 
     char temp = Strings[i]; 
     Strings[i] = Strings[i+1]; 
     Strings[i+1] = temp; 
     j = length[i]; 
     } 
     } 
    } 

行if(Strings [i] [j]> Strings [i + 1] [j])是卡住的地方。正如我已經教過的,二維數組中的第一個支架包含字符串,第二個支架指向字符?我不確定如何去比較這些字符串的字符。很確定,試圖做到這一點,因爲我(像我會數字)關閉。

另外,不知道這是否與我上面的代碼有關(由於它未完成或因爲temp是char類型而Strings是char類型的二維數組;但是我得到一個指向equals的錯誤登錄下面的語句:

error: incompatible types when assigning to type 'char[1000]' from type 'char' 
      Strings[i+1] = temp; 

編輯:在我的代碼現在看,我可以看到一個巨大的缺陷,它會在字符串相同指數在運行到一個字[X]進來後性格字符串[X + 1],然後更改字符串一起;這是不對的,我會改變我的代碼來糾正,但我仍然不知道如果我比較字符以正確的方式

+0

是否允許*非字符串*函數?如'qsort()'。如果他們是,那可能會讓你更容易(雖然授予,'qsort()'可能會令人困惑)。 – 2015-02-10 01:54:27

+0

另外,我們可以看看你如何聲明'字符串? – 2015-02-10 01:57:26

+0

@TimČas我將Strings聲明爲'Char Strings [TotalStrings] [MaxLength]'。非字符串函數沒有問題,但我正試圖學習如何編寫一個循環來檢查每個單獨的字符(如果有必要) – newJavaUser 2015-02-10 02:02:03

回答

0

修訂(見下文)
關於錯誤:
看起來
char temp = Strings[i];
應該是:
char *temp = Strings[i];
我不知道爲什麼,當你分配一個編譯器沒有錯誤字符變量與指針,但這是看起來是問題。 我建議在C中閱讀pointers,特別是如果你來自Java。

關於您的原始問題:
看起來您正在比較正確的方式。
另外,是的,正如你已經教過的,二維數組中的第一個括號包含字符串,第二個括號指向字符。

UPDATE:
正如喬納森說,溫度實際上應該是:
char temp[1000];
但因爲你沒有使用標準庫函數,你需要手動複製字符串。

+0

實際上,它在我看來更像'temp'應該是'char temp [1000];'而賦值應該是字符串複製操作。 – 2015-02-10 04:30:25

+0

更新後,謝謝@JonathanLeffler,你是對的。 – 2015-02-10 16:21:49

1

由於@Nunzio Tocci已經顯示在你的代碼的問題。

在你的代碼的問題是, 指定字符指針爲char變量,反之亦然。

您可以通過將函數分解爲函數來改善代碼,比如一個用於比較字符串,另一個用於對它進行排序,因爲您也不能使用strcpy(因爲您說沒有標準庫調用),所以可以編寫函數字符串複製,並實現代碼。

下面是一個例子,你可以看看它並從這裏開始。

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

// Compare two strings, returns -1, 0 ,1 
int string_compare(const char* s1, const char* s2) { 
    // perfrom null dereferencing error checking 
    while((*s1==*s2) && *s1) 
     s1++,s2++; 
    return *s1 < *s2 ? -1 : *s1 > *s2; 
} 

// Copy s2 into s1 
int string_copy(char *s1, const char* s2){ 
    // perfrom null dereferencing error checking 
    while((*s1++ = *s2++)) 
     ; 
    return 0; 
} 

// Sort the array of string 
char *const *const string_sort(char *const *const str, const int  totalString,const int strMaxLen) { 

    char *temp = malloc(strMaxLen); // Using temp for swapping 
    int i,j; 

    for (i = 0; i < totalString; i++) { 
     for (j = 0; j < totalString - 1; j++) { 
      if (string_compare(str[j], str[j + 1]) > 0) { 
       string_copy(temp,str[j]); 
       string_copy(str[j],str[j + 1]); 
       string_copy(str[j + 1],temp); 
      } 
     } 
    } 
    free(temp); 
    return str; 
} 

int main() { 
    /* No of strings/ can be known at runtime also 
     since we are not using array */ 
    const int totalString = 10; 
    const int strLen = 1000; // string length 

    // Pointer to store address of an the array of string. 
    char **str = NULL; 
    int i; 

    /* Allocate memory for storing the 
     address of "totalString" no of stirngs */ 
    str = malloc(totalString*sizeof(char*)); 

    /* allocate memory to store a string 
     witb 'strLen' length */ 
    for(i = 0; i < totalString; ++i) 
     str[i] = malloc(strLen); 

    /* Read from the user or 
     any external source (ex: file) */ 
    for(i = 0; i < totalString; ++i) 
     scanf("%s",str[i]); 

    /* sort the strings */ 
    string_sort(str,totalString,strLen); 

    /* print then */ 
    for(i = 0; i < totalString; ++i) 
     printf("%s\n",str[i]); 

    for(i = 0; i < totalString; ++i) 
     free(str[i]); 
    free(str); 
    return 0; 
} 
+0

你能解釋一下原代碼中的缺陷是什麼,你的代碼有什麼不同,以及它爲什麼會起作用?在沒有任何評論的情況下轉儲一段代碼並不是很有幫助。 – nwellnhof 2015-02-10 10:10:10

+0

注意:'while&(* s1 && * s2 &&(* s1 == * s2)'''不需要'&& * s2''。建議首先放置'* s1 == * s2':'while((* s1 == * s2)&& * s1)'。 – chux 2015-02-10 18:57:05

+0

@chux我同意不需要'&& * s2',但'(* s1 == * s2)'的順​​序並沒有什麼區別。 – Sridhar 2015-02-11 07:33:17

相關問題