-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab4.cpp
More file actions
70 lines (62 loc) · 1.46 KB
/
Lab4.cpp
File metadata and controls
70 lines (62 loc) · 1.46 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
/*
* Lab4.cpp
*
* Created on: Oct 3, 2013
* Author: leon
*/
#include <cstdlib>
#include <string>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
string delete_all(const string& s, const string& junk) {
string result = s;
size_t found = result.find(junk);
while (found != string::npos) {
result.erase(found, junk.length());
found = result.find(junk);
}
return result;
}
bool is_junk(char c) {
return isspace(c);
}
string ltrim(const string& s, bool (*is_junk)(char)) {
size_t startpos = 0;
while (startpos != string::npos && is_junk(s[startpos++]))
;
return s.substr(startpos - 1);
}
string rtrim(const string& s, bool (*is_junk)(char)) {
size_t stoppos = s.length();
while (stoppos != 0 && is_junk(s[--stoppos]))
;
return s.substr(0, stoppos + 1);
}
string squeeze(const string& s, char c) {
string result = s;
size_t found = result.find(c);
while (found++ != string::npos) {
while (found != result.length()) {
if (result[found] != c)
break;
result.erase(found, 1);
}
found = result.find(c, found);
}
return result;
}
int lab4_main(int argc, char** argv) {
string s("a123kflkadfj123kjlajfkd123kkkk");
string junk("123");
cout << delete_all(s, junk) << endl;
cout << ltrim(" \t ", is_junk) << endl;
cout << rtrim("012345 \t ", is_junk) << endl;
cout << squeeze("a1111gdsga11111gggdsg11111", '1') << endl;
return 0;
}