Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. bool String::containsStr(const String& substr) const
  2. {
  3. if (strlen(substr.str) > strlen(this->str))
  4. {
  5. return false;
  6. }
  7.  
  8. for (int i = 0; i < strlen(this->str); i++)
  9. {
  10. if (this->str[i] == substr.str[0] && strlen(substr.str) == 1)
  11. {
  12. return true;
  13. }
  14.  
  15. if (this->str[i] == substr.str[0] && strlen(substr.str) > 1)
  16. {
  17. for (int j = 1; j < strlen(substr.str); j++)
  18. {
  19. if (this->str[i+j] != substr.str[j])
  20. {
  21. break;
  22. }
  23. return true;
  24. }
  25. }
  26. }
  27. return false;
  28. }
  29.  
  30. void String::operator-(const String& other)
  31. {
  32. if (this->containsStr(other))
  33. {
  34. char* smallerBuffer = new char[strlen(this->str) - strlen(other.str) + 1];
  35. int index = getIndexSubString(this->str, other.str);
  36. int nextindexStr = index + strlen(other.str);
  37. int nextindex = 0;
  38.  
  39. for (int i = 0; i < index; i++)
  40. {
  41. smallerBuffer[i] = this->str[i];
  42. nextindex = i + 1;
  43. }
  44. int subtractor = strlen(this->str) - nextindexStr;
  45.  
  46. for (int i = 0; i < subtractor; i++)
  47. {
  48. smallerBuffer[nextindex] = this->str[nextindexStr];
  49. nextindex++;
  50. nextindexStr++;
  51. }
  52.  
  53. smallerBuffer[strlen(this->str) - strlen(other.str)] = '\0';
  54. this->erase();
  55. this->str = smallerBuffer;
  56. }
  57. else
  58. {
  59. std::cout << other.str << " is not a substring of " << this->str;
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement