简体   繁体   English

删除 char 数组中的字符

[英]Deleting characters in a char array

#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)
{ 
}*/`

I have to put the for in the "delete" function calling the value B and print it in main, do you know how to do it?我必须将 for 放入“删除” function 调用值 B 并在 main 中打印,你知道怎么做吗?


Given a string A read from the keyboard, create a function in C ++ language that calculates a second string B obtained from the first by deleting all the characters that appear more than once.给定从键盘读取的字符串 A,在 C ++ 语言中创建一个 function,该语言通过删除所有出现多次的字符来计算从第一个字符串获得的第二个字符串 B。 The resulting string must therefore contain the characters of the first string, in the same order, but without repetitions.因此,生成的字符串必须以相同的顺序包含第一个字符串的字符,但不能重复。

Instead of 'cout', you just store the characters into a new string, and increment its index, see code below as an example:而不是 '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;
}

Output: Output:

Insert string
lalalgdgdfsgwwyrtytr
lagdfswyrt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM