2016-11-13 47 views
-2

我正在努力爲我的課程進行分配,我們必須將用戶輸入的內容輸入到字符數組(字)中,並在需要時輸入所輸入字中的重複項。將修改後的數組(單詞)數組與字母數組(abc)進行比較,以從列表中刪除重複的字母。一旦刪除了重複項,只需將修改後的單詞輸出到newAbc數組中即可。2個字符數組之間的重複排序

例如:

HELLO將首先成爲HELO然後從新的陣列進行比較來字母表的端輸出應該HELOABCDFGIJKMNPQRSTUVXYZ後的字。

我被困在比較新單詞到字母表的for循環上。

char word[20], newAbc[40] = { '\0' }; 
    char abc[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 
    int i = 0, b = 1, n = 0, leng, dup; 
    //dup counts up the repeats but is established in the first portion of the program but i've excluded it as it works perfectly. 

    cout << "Please enter a word: "; 
    cin >> word; 

    leng = strlen(word); 

    b = 0; 
    n = leng - dup; 
    i = 0; 

    for (i = 0; i < n; i++) 
    { 
     for (b = 0; b < 27; b++) 
     { 
      if (newAbc[i] != abc[b]) 
      { 
       newAbc[n] = abc[b]; 
       n++; 
      } 
     } 
    } 

    for (i = 0; i < 27; i++) 
     cout << newAbc[i]; 
    cout << endl; 

    return 0; 
} 

我很感激任何對我的錯誤的洞察力。

+1

該代碼應該給你的警告有關多字符字符文字。而不是「/ 0」,你需要一個像'\ 0'這樣的反斜槓。 –

+0

http://en.cppreference.com/w/cpp/algorithm/set_difference –

+0

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

回答

0

崩潰的主要問題是您正在更改n for循環在newAbc中迭代。如果條件成立至少25次,則在每次迭代中將n遞增25(最小值),導致訪問超出限制的存儲器(SEG-FAULT)。

for (i = 0; i < n; i++) 
    { 
     for (b = 0; b < 27; b++) 
     { 
      if (newAbc[i] != abc[b]) // this condition is not correct 
      { // this will be true atleast 25 times 
       newAbc[n] = abc[b]; // wrong memory access 
       n++; // here is the problem 
      } 
     } 
    } 

假設您的重複計數正常工作,下面需要的變化: -

char word[20]; 

    // FIXME: your new array should not contain no duplicate so size can be 27 
    char newAbc[40] = {'\0'}; 

    // FIXME: simply can be char abc[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    char abc[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 

    cout << "Please enter a word: "; 
    cin >> word; 

    // dup counts up the repeats but is established in the first portion of 
    // the program but i've excluded it as it works perfectly. 

    // Lets say word = "HELLO"; so dup = 1, and newArray, should have "HELO" 
    memcpy(newAbc, "HELO", 4); // from your excluded part of code 
    int dup = 1; // from your excluded part of code 

    int leng = strlen(word); // length of input word 
    int n = leng - dup; // char remained to be inserted 

    // iterator for new array(newAbc) 
    int c = n; // n elements are already there 

    // Just reversed your loop 
    for (int b = 0; b < 27; b++) 
    { 
     int found = 0; 
     for (int i = 0; i < n; i++) 
     { 
      if (newAbc[i] == abc[b]) 
      { 
       found = 1; 
       break; 
      } 
     } 

     if (!found) 
     { 
      newAbc[c] = abc[b]; 
      c++; 
     } 
    } 

    for (int i = 0; i < 27; i++) 
     cout << newAbc[i]; 
    cout << endl; 

    return 0; 
+1

非常感謝您在調整完所有內容之後,如您所說的那樣完美。對此,我真的非常感激! – Dtrain327