2016-11-13 8 views
-2

私は、ユーザからの文字配列(単語)への入力を取り、必要に応じて入力された単語の重複を取り除かなければならないクラスの代入に苦労しています。修正された配列(word)とアルファベット配列(abc)の配列を比較して、繰り返し文字をリストから削除します。重複が削除されたら、newAbc配列に新しい形式のアルファベットが続く修飾された単語を出力するだけです。例えば2つの文字配列の間で重複をソートする

:こんにちは最初の新しい配列からエンド出力はHELOABCDFGIJKMNPQRSTUVXYZであるべきであるアルファベットを比較した後、次いでHELOなる

ワード。

新しい単語をアルファベットと実際に比較する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

クラッシュの主な問題は、newAbcに反復するためにforループ内でnを変更していることです。あなたのif条件は少なくとも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

関連する問題