I am writing a program where the user enters 2 strings and the program checsk to see if any characters are in both strings, and if they are, remove them from the first string and print it.

So i have this at the moment, and it's almost working but it seems to be ignoring the first character of the second string. If you enter "computer" and "program", for example, it returns "cpute" instead of "cute".

Anyone have any ideas?

#include <iostream>
#include <string>

using namespace std;

int i;
int j;
string x;
string y;

int main()
{   
            
        
        cout << "\n Enter a word \n";
        getline (cin, x);
        
        cout << "\n Enter another word \n";
        getline (cin, y);
        
        int z = x.length();
        int w = y.length();
        
    
        for (i = 0; i < z; ++i)
        {
            for (j = 0; j < w; ++j)
            {
                if(x[i] == y[j])
                { 
                   x.erase (i,1); 
                }
            }
        }
   
        cout << "Result:" << x << "\n";
     
        system ("pause");
        return 0;
}

>> it's almost working but it seems to be ignoring the first character of the second string.

Throw more stuff at it till you can verify this. Scramble the word "computer" and see if it always misses the same letter(s).

Instead of "computer", use strings like "abcdefghij" and "jihgfedcba" until you see a pattern of bad behavior.

>> it's almost working but it seems to be ignoring the first character of the second string.

Throw more stuff at it till you can verify this. Scramble the word "computer" and see if it always misses the same letter(s).

Instead of "computer", use strings like "abcdefghij" and "jihgfedcba" until you see a pattern of bad behavior.

Thanks for the advice.

Well, for "abcdefg" and "gfedcba" it returned "bdf", and for "opterumc" and "program" it returned "pteuc". On the other hand "poterumc" and "program" returns "teuc", which is what I want. It ignores the first character a lot but "abcde" and "bcdef" work perfectly and return "a".

What do you make of it?

What do you make of it?

It's not the SECOND string, it's the FIRST string that's the problem.

You aren't REPLACING characters, you are DELETING characters, which means the lengths change and the characters shift. Your program/algorithm doesn't take this into consideration, so letters in the first string get skipped and hence aren't deleted.

It's not the SECOND string, it's the FIRST string that's the problem.

You aren't REPLACING characters, you are DELETING characters, which means the lengths change and the characters shift. Your program/algorithm doesn't take this into consideration, so letters in the first string get skipped and hence aren't deleted.

Ahh, I see. Don't think I would have gotten that. The code works now, thank you very much for your help.

can someone please post.. whats the correction here ????
i am unable to get it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.