I'm doing a project for the university that involves object oriented programming . I must create objects (with value string
) and add them, subtract them and multiply them. My problem is in subtraction. I'm supposed to iterate between each element of the string
entered in B and, if I find that that element has some element of string
A, delete it. In short, return the letters that do not repeat between both strings.
This is the code that I've been doing so far, but when I run it, it returns a blank space .
FunnyString operator - (FunnyString anotherMin)
{
FunnyString finalWord;
for (int i=0;i<(anotherMin.word).length();i++)
for (int j=0;j<word.length();j++)
if (anotherMin.word[i]==word[j])
{
(anotherMin.word).erase((anotherMin.word).begin() + i);
}
else
{
finalWord.word+=word[j];
}
return finalWord;
}
Could you help me out? By the way I already overloaded the symbol + so there is no problem when building the objects as strings. It's just in the subtraction code.
Here I introduce the complete FunnyString class:
class FunnyString
{
public:
string word;
FunnyString()
{};
FunnyString (char x[])
{
word=x;
}
FunnyString operator + (FunnyString anotherPlus)
{
FunnyString finalWord;
if ((word[word.length()-1])==(anotherPlus.word[0]))
{
word.erase(word.begin() + word.length()-1);
(anotherPlus.word).erase((anotherPlus.word).begin());
finalWord.word= word + anotherPlus.word;
}
else
{
finalWord.word= word + anotherPlus.word;
}
return finalWord;
}
FunnyString operator - (FunnyString anotherMin)
{
FunnyString finalWord;
for (int i=0;i<(anotherMin.word).length();i++)
for (int j=0;j<word.length();j++)
if (anotherMin.word[i]==word[j])
{
(anotherMin.word).erase((anotherMin.word).begin() + i);
}
else
{
finalWord.word+=word[j];
}
return finalWord;
}
};