Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool String::containsStr(const String& substr) const
- {
- if (strlen(substr.str) > strlen(this->str))
- {
- return false;
- }
- for (int i = 0; i < strlen(this->str); i++)
- {
- if (this->str[i] == substr.str[0] && strlen(substr.str) == 1)
- {
- return true;
- }
- if (this->str[i] == substr.str[0] && strlen(substr.str) > 1)
- {
- for (int j = 1; j < strlen(substr.str); j++)
- {
- if (this->str[i+j] != substr.str[j])
- {
- break;
- }
- return true;
- }
- }
- }
- return false;
- }
- void String::operator-(const String& other)
- {
- if (this->containsStr(other))
- {
- char* smallerBuffer = new char[strlen(this->str) - strlen(other.str) + 1];
- int index = getIndexSubString(this->str, other.str);
- int nextindexStr = index + strlen(other.str);
- int nextindex = 0;
- for (int i = 0; i < index; i++)
- {
- smallerBuffer[i] = this->str[i];
- nextindex = i + 1;
- }
- int subtractor = strlen(this->str) - nextindexStr;
- for (int i = 0; i < subtractor; i++)
- {
- smallerBuffer[nextindex] = this->str[nextindexStr];
- nextindex++;
- nextindexStr++;
- }
- smallerBuffer[strlen(this->str) - strlen(other.str)] = '\0';
- this->erase();
- this->str = smallerBuffer;
- }
- else
- {
- std::cout << other.str << " is not a substring of " << this->str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement