13485 String Calculator(class)

                Never    
C++
       
#include <iostream>
#include<string>
#include"function.h"

    String_Calculator::String_Calculator(const string str) : s(str) {}
    String_Calculator& String_Calculator::Add(const string str){
        s += str;
        return *this;
    }
    String_Calculator& String_Calculator::Subtract(const string str){
        size_t pos = s.find(str);
        if(pos != string::npos) {
            s.erase(pos , str.length());
        }
        else {
            s = "error";
        }
        return *this;
    }
    String_Calculator& String_Calculator::DividedBy(const string str){
        size_t pos = 0;
        size_t cnt = 0;
        while ((pos = s.find(str, pos)) != string::npos) {
        cnt++;
        pos += str.length();
    }
        s = to_string(cnt);
        return *this;
    }
    void String_Calculator::output() const{
        cout << s << "\n";
    }

Raw Text