我正在努力爲我的課程進行分配,我們必須將用戶輸入的內容輸入到字符數組(字)中,並在需要時輸入所輸入字中的重複項。將修改後的數組(單詞)數組與字母數組(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;
}
我很感激任何對我的錯誤的洞察力。
該代碼應該給你的警告有關多字符字符文字。而不是「/ 0」,你需要一個像'\ 0'這樣的反斜槓。 –
http://en.cppreference.com/w/cpp/algorithm/set_difference –
解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –