-
Notifications
You must be signed in to change notification settings - Fork 0
/
test27_29.cpp
75 lines (68 loc) · 1.62 KB
/
test27_29.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
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<ctype.h>
#include<string>
#include<map>
using namespace std;
void removeSubstring(string& s, string t) {
string result;
int i = 0;
while (i < s.length()) {
bool match = true;
for (int j = 0; j < t.length(); j++) {
if (s[i + j] != t[j]) {
match = false;
break;
}
}
if (match) {
i += t.length();
}
else {
result += s[i];
i++;
}
}
s = result;
}
void countCharacters(string s) {
map<char, int> counts;
for (int i = 0; i < s.length(); i++) {
counts[s[i]]++;
}
for (auto it = counts.begin(); it != counts.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
}
void findSubstring(string s, string t) {
vector<int> positions;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.substr(i, t.length()) == t) {
count++;
positions.push_back(i);
}
}
cout << "Count: " << count << endl;
cout << "Positions: ";
for (int i = 0; i < positions.size(); i++) {
cout << positions[i] << " ";
}
cout << endl;
}
int main() {
string s = "hello worldlllldadad";
string t = "l";
/*cout << s << endl;
cout << "removeSubstring" << endl;
removeSubstring(s, t);
cout << s << endl;*/
/*cout << "countCharacters" << endl;
countCharacters(s);*/
cout << s << ' ' << t << endl;
cout << "findSubstring" << endl;
findSubstring(s, t);
return 0;
}