-1
#include <iostream>
#include <string.h>
#include <algorithm>
# define N 100
    
using namespace std;
    
int main()
{
    char A[N];
    unsigned char APP[256] = {0};
    cout << "Insert string" << endl;
    cin.getline(A,100);
    for(int i=0; i < strlen(A); ++i)
    {
        unsigned char B = A[i];
        if(!APP[B])
        {
            ++APP[B];
            cout << B;
        }
    }
    return 0;
}

/*char eliminazione(char,char)
{ 
}*/`

我必须将 for 放在调用值 B 的“删除”函数中并在 main 中打印,你知道怎么做吗?


给定从键盘读取的字符串 A,用 C++ 语言创建一个函数,该函数通过删除所有出现多次的字符来计算从第一个字符串 B 获得的第二个字符串 B。因此,生成的字符串必须以相同的顺序包含第一个字符串的字符,但不能重复。

4

1 回答 1

-1

而不是 'cout',您只需将字符存储到一个新字符串中,并增加其索引,请参见下面的代码作为示例:

#include <iostream>
#include <string.h>
#include <algorithm>
# define N 100

using namespace std;

int main()
{
    unsigned char A[N]; // better to have same type for both A and B
    unsigned char B[N];
    memset(B, 0, sizeof(B));
    unsigned char APP[256] = {0};
    cout << "Insert string" << endl;
    cin.getline(A,100);
    int j = 0;
    for(int i=0; i < strlen(A); ++i)
    {
        unsigned char c = A[i];
        if(!APP[c]++)
            B[j++] = c; //cout << c;
    }
    cout << B << endl;
    return 0;
}

输出:

Insert string
lalalgdgdfsgwwyrtytr
lagdfswyrt
于 2021-03-24T00:58:16.137 回答