-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_replace.cpp
56 lines (45 loc) · 1.32 KB
/
str_replace.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
#include <iostream>
#include <string>
using namespace std;
string& str_replace(const string &search, const string &replace, string &subject);
int main(int argc, char *argv[])
{
string str;
//test basic replace
str ="I have a lovely bunch of cocoa nuts";
cout << str << endl;
cout << str_replace(" ", " ;", str) << endl;
//test removal of spaces
str ="I have a lovely bunch of cocoa nuts";
cout << str << endl;
cout << str_replace(" ", "", str) << endl;
//this test will generate an infinite loop if not done in 1 pass
str ="I have a lovely bunch of cocoa nuts";
cout << str << endl;
cout << str_replace(" ", " a", str) << endl;
return 0;
}
string& str_replace(const string &search, const string &replace, string &subject)
{
string buffer;
int sealeng = search.length();
int strleng = subject.length();
if (sealeng == 0)
return subject;//no change
for (int i = 0, j = 0; i < strleng; j = 0)
{
while (i + j <strleng && j < sealeng && subject[i+j] == search[j])
j++;
if (j == sealeng)//found 'search'
{
buffer.append(replace);
i += sealeng;
}
else
{
buffer.append( &subject[i++], 1);
}
}
subject = buffer;
return subject;
}