Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include "MyString.h"
  2.  
  3. bool operator == (const MyString& S1, const MyString& S2)
  4. {
  5. if (S1.length == S2.length)
  6. {
  7. short counter1 = S1.length;
  8. int mis = 0;
  9. int i = 0;
  10. while (i != counter1)
  11. {
  12. if (S1.str[i] != S2.str[i]) { mis++; }
  13. i++;
  14. }
  15. if (mis != 0) { return false; }
  16. else { return true; }
  17. }
  18. else { return false; }
  19.  
  20. }
  21. ostream& operator <<(ostream& OS, const MyString& S)
  22. {
  23. OS << S.str;
  24. return OS;
  25. }
  26.  
  27. istream& operator >>(istream& IS, MyString& S)
  28. {
  29. char* input = new char[100];
  30. IS.getline(input, 100);
  31.  
  32. int i = 0;
  33. while (input[i] != '\0')
  34. {
  35. i++;
  36. }
  37. S.length = i + 1;
  38. delete[]S.str;
  39. S.str = new char[(i + 1)];
  40. for (int j = 0; j < (i); j++)
  41. {
  42. S.str[j] = input[j];
  43. }
  44. S.str[i] = '\0';
  45. delete[]input;
  46. return IS;
  47. }
  48. bool MyString::isEmpty()
  49. {
  50. if (str[0] == NULL && length == 1)
  51. {
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. int find(const MyString& S1,const MyString& S2)
  60. {
  61. int index = 0;
  62. int strsize1 = S1.length;
  63. int strsize2 = S2.length;
  64. int counter = 0;
  65. int checker = 0;
  66. for (int i = 0;checker!=strsize2 || i<strsize1;i++)
  67. {
  68. if (S2.str[checker] == S1.str[i])
  69. {
  70. checker++;
  71. index = i;
  72. }
  73. if (checker > 0 && S2.str[checker] == S1.str[i] && checker != (i - 1))
  74. {
  75. return -1;
  76. }
  77. }
  78. index =index-checker+1;
  79. if (checker == strsize2)
  80. {
  81. return index;
  82. }
  83. else
  84. {
  85. return -1;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement