Subtract a string

1

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;
}

};
    
asked by Roger Urrutia Parker 14.05.2018 в 08:23
source

1 answer

1
  

Iterate between each element of the string entered in B and, if I find that that element has an element coinciding with string A, delete it.

You have not established what should happen if you try to subtract a long string from a short one or if the operation should be commutative , so I'll make the most convenient proposal to implement using the header <algorithm> and its utilities std::copy_if and std::find .

Proposal.

At the pseudocode level, the steps to follow would be:

  • Identify the shortest and longest chain.
  • Walk the string long character by character
  • Copy, to a new string, the characters that are not in the short string.
  • A possible implementation might look like:

    std::string resta(const std::string &a, const std::string &b)
    {
        std::string result;
        // Identificar la cadena más corta y más larga.
        const std::string &largo = a.size() < b.size() ? b : a;
        const std::string &corto = a.size() < b.size() ? a : b;
    
        // El resultado será, como mucho, tan largo como la cadena corta.
        result.reserve(corto.size());
        // Copiamos el contenido de 'largo' que cumpla la lambda
        std::copy_if(largo.begin(), largo.end(), std::back_inserter(result), [&corto](const std::string::value_type &c)
        {
            // Verdadero sólo si 'corto' no contiene 'c'.
            return std::find(corto.begin(), corto.end(), c) == corto.end();
        });
    
        return result;
    }
    

    You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

    You could also design the algorithm backwards if you copy the long string in result and delete the letters that are in the long string, for this it would be useful std::remove_if .

        
    answered by 14.05.2018 в 10:58