-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.cpp
84 lines (75 loc) · 1.54 KB
/
string.cpp
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
#include "string.h"
unsigned strlength(const char *a)
{
int i = 0;
while (a[i] != 0)
{
i++;
}
return i;
}
int strcmp(const char* s1, const char* s2)
{
while (*s1 && (*s1 == *s2))
s1++, s2++;
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
void strcopy(char *&b, const char *a)
{
if (b != nullptr)
delete[] b;
b = nullptr;
if (a == nullptr)
return;
int length = strlength(a);
b = new char[length + 1];
for (int i = 0; i < length; i++)
{
b[i] = a[i];
}
b[length] = 0;
}
char *String::get_content() { return content; }
void String::set_content(char buf[]){
strcopy(content, buf);
len = sizeof(buf)/sizeof(buf[0]);
}
String::String() : content(nullptr), len(0) {}
String::String(const char *a) : content(nullptr)
{
strcopy(content, a);
len = strlength(a);
}
String::String(const String &other) : content(nullptr)
{
strcopy(content, other.content);
len = other.len;
}
String::~String()
{
if (content != nullptr)
delete[] content;
}
char &String::operator[](int a)
{
return content[a];
}
unsigned String::length() const
{
return len;
}
ostream &operator<<(ostream &os, String &s)
{
if (s.content != nullptr)
cout << s.content;
return os;
}
istream &operator>>(istream &in, String &a)
{
int size = 1024;
char *arr = new char[size];
in >> arr;
a.set_content(arr);
delete arr;
return in;
}