-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
128 lines (111 loc) · 2.35 KB
/
String.cpp
File metadata and controls
128 lines (111 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "String.h"
char* String::Gets()
{
char ch;
int n = 0;
char* str = nullptr;
while ((ch = getchar()) != '\n')
{
str = (char*)realloc(str, n + 1);
str[n] = ch;
n++;
}
str = (char*)realloc(str, n + 1);
str[n] = 0;
return str;
}
String::String()
{
this->str = nullptr;
}
String::String(const String& CopyStr)
{
this->str = (char*)malloc(strlen(CopyStr.str) + 1);
strcpy_s(this->str, strlen(CopyStr.str) + 1, CopyStr.str);
}
String& String::operator=(const String& CopyStr)
{
if (this != &CopyStr)
{
if (this->str != nullptr)
free(this->str);
this->str = (char*)malloc(strlen(CopyStr.str) + 1);
strcpy_s(this->str, strlen(CopyStr.str) + 1, CopyStr.str);
}
return *this;
}
void String::Vvod()
{
cout << "Vidit text:\n";
if (this->str != nullptr)
free(this->str);
this->str = Gets();
}
void String::Show()
{
cout << this->str << "\n";
}
String& String::operator++()
{
int len = strlen(this->str) + 1;
this->str = (char*)realloc(this->str, len + 1);
str[len - 1] = ' ';
str[len] = '\0';
return *this;
}
String String::operator++(int p)
{
String tmp = *this;
int len = strlen(this->str) + 1;
this->str = (char*)realloc(this->str, len + 1);
str[len - 1] = ' ';
str[len] = '\0';
return tmp;
}
String& String::operator--()
{
int len = strlen(this->str);
this->str[len - 1] = '\0';
this->str = (char*)realloc(str, len + 1);
return *this;
}
String String::operator--(int p)
{
String tmp = *this;
int len = strlen(this->str);
this->str[len - 1] = '\0';
this->str = (char*)realloc(str, len + 1);
return tmp;
}
char& String::operator[](int i) const
{
if (i < 0)
{
cout << "Indeks za megamu stroku. 1 element = ";
return this->str[0];
}
if (i > strlen(this->str) - 1)
{
cout << "Indeks za megamu stroku. "<< strlen(this->str) - 1<<" element = ";
return this->str[strlen(this->str) - 1];
}
return this->str[i];
}
int String::operator()(char ch, int l, int r)
{
for (int i = l; i <= r; i++)
{
if (this->str[i] == ch)
return i;
}
return -1;
}
String::operator int()
{
return strlen(this->str);
}
String::~String()
{
free(this->str);
this->str = nullptr;
}