-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.cpp
195 lines (191 loc) · 3.93 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef struct
{
char *ch;
int length;
}String;
void strAssign(String &str, const char *chars)
{
int len = strlen(chars);
if(!len)
{
str.ch = NULL;
str.length = 0;
}
else
{
if(!(str.ch=new char[len]))
{
cerr << "memory allocation failed!" << endl;
exit(-1);
}
else
{
for(int i=0; i<len; i++)
str.ch[i] = chars[i];
str.length = len;
str.ch[len] = '\0';
}
}
}
int strLength(const String &str)
{
return str.length;
}
int strCompare(const String &str1, const String &str2)
{
int i;
for(i=0; i<str1.length&&i<str2.length; i++)
{
if(str1.ch[i] != str2.ch[i])
break;
}
return str1.ch[i] - str2.ch[i];
}
void clearString(String &str)
{
if(str.ch==NULL)
return;
delete[] str.ch;
str.ch = NULL;
str.length = 0;
}
void display(const String &str)
{
if(str.ch==NULL)
{
cout << "the current string is empty." << endl;
return;
}
cout << "the current string is: " << str.ch << endl;
cout << "the length is: " << str.length << endl;
}
void strConcat(String &newstr, const String &str1, const String &str2)
{
if(!(newstr.ch=new char[str1.length+str2.length]))
{
cerr << "memory allocation failed!" << endl;
exit(-1);
}
int i;
for(i=0; i<str1.length; i++)
newstr.ch[i] = str1.ch[i];
for(i=0; i<str2.length; i++)
newstr.ch[i+str1.length] = str2.ch[i];
newstr.ch[i+str1.length] = '\0';
newstr.length = str1.length + str2.length;
}
void subString(String &sub, const String &str, int pos, int len)
{
if(pos<0 || pos>=str.length || len<0 || len>str.length-pos)
{
sub.ch = NULL;
sub.length =0;
cerr << "error!" << endl;
return;
}
if(!len)
{
sub.ch = NULL;
sub.length = 0;
cout << "the substring is empty!" << endl;
return;
}
else
{
if(!(sub.ch=new char[len]))
{
cerr << "memory allocation failed!" << endl;
exit(-1);
}
int i;
for(i=0; i<len; i++)
sub.ch[i] = str.ch[pos+i];
sub.length = len;
sub.ch[i] = '\0';
}
}
void reallocString(String &str, int len)
{
char * pt = new char[len+str.length];
int i;
for(i=0; i<str.length; i++)
pt[i] = str.ch[i];
pt[i] = '\0';
delete[] str.ch;
str.ch = pt;
}
void strInsert(String &str1, int pos, String &str2)
{
if(pos<0 || pos>str1.length)
{
cerr << "error!" << endl;
return;
}
else if(str2.ch==NULL)
return;
else if(str1.ch==NULL)
strAssign(str1, str2.ch);
else
{
reallocString(str1, str2.length);
for(int i=str1.length-1; i>=pos; i--)
str1.ch[i+str2.length] = str1.ch[i];
str1.length += str2.length;
for(int i=0; i<str2.length; i++)
str1.ch[pos+i] = str2.ch[i];
str1.ch[str1.length] = '\0';
}
}
int strMatch(String &str1, String &str2)
{
int i=0, j=0, v;
while(i<str1.length && j<str2.length)
{
if(str1.ch[i] == str2.ch[j])
{
i++;
j++;
}
else
{
i = i - j + 1;
j = 0;
}
}
if(j >= str2.length)
v = i - j;
else
v = -1;
return v;
}
int KMPnext(String &str)
{
int j = -1, i = 0;
int next[17];
next[0] = -1;
while(i < str.length)
{
if(j == -1 || str.ch[i] == str.ch[j])
{
i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
}
}
int main()
{
String str1, str2;
strAssign(str1, "zhanga");
strAssign(str2, "zhang");
cout << strCompare(str1, str2);
return 0;
}