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

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?


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. The resulting string must therefore contain the characters of the first string, in the same order, but without repetitions.

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
Pepp
  • 9
  • 1
    These are C-strings. A C++ string would include `` and be declared with type `std::string`. – sweenish Mar 24 '21 at 00:24
  • Please post questions in English. This program looks like it's intended to output each unique letter of input once, in the order in which the letters arrive. I don't know what problem you're having with your code, and it's not at all clear what you mean by _"put the for in the delete function calling the value B and print it in main"_ – paddy Mar 24 '21 at 00:52
  • my code works and only that the "delete" function is empty, the exercise asks me to write a function, in my case I wrote the procedure in the for but I can't and put it in the form of a function – Pepp Mar 24 '21 at 00:57
  • This question is very similar to another question asked just yesterday: [Is there a way to delete a repeated character in a string using pointers in C?](https://stackoverflow.com/questions/66755490/) – Remy Lebeau Mar 24 '21 at 02:08

1 Answers1

-1

Instead of 'cout', you just store the characters into a new string, and increment its index, see code below as an example:

#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:

Insert string
lalalgdgdfsgwwyrtytr
lagdfswyrt
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81