【问题标题】:Sorting out duplicates between 2 char arrays [closed]整理 2 个字符数组之间的重复项 [关闭]
【发布时间】: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; 
} 

如果能对我的错误提出任何见解,我将不胜感激。

【问题讨论】:

标签: c++ arrays sorting char


【解决方案1】:

崩溃的主要问题是您在 for 循环中更改 n 以在 newAbc 中进行迭代。并且您的 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; 

【讨论】:

  • 谢谢你,按照你说的调整一切后它工作得很好。我真的很感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
相关资源
最近更新 更多