【发布时间】:2017-03-27 07:49:58
【问题描述】:
我正在为我的班级分配一项作业,我们必须将用户的输入输入到一个字符数组(单词)中,并在需要时取出输入的单词中的重复项。将修改后的数组 (word) 的数组与字母数组 (abc) 进行比较,以从列表中删除重复的字母。删除重复项后,只需将修改后的单词和字母表的新形式输出到 newAbc 数组中。
例如:
单词 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'这样的反斜杠。 -
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。