-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoveCharacters.cpp
47 lines (37 loc) · 1.11 KB
/
RemoveCharacters.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
// GeeksforGeeks https://www.geeksforgeeks.org/remove-characters-from-the-first-string-which-are-present-in-the-second-string/
#include <iostream>
#include <string>
#include <array>
const int SIZE = 256;
std::array<int, SIZE> getCharacterFrequency(const std::string& str)
{
std::array<int, SIZE> count {};
for (int i = 0; i < str.size(); ++i) {
count[str[i]]++;
}
return count;
}
std::string removeMaskedCharacters(const std::string& str, const std::string& mask)
{
std::array<int, SIZE> count = getCharacterFrequency(mask);
std::string result = "";
for (int i = 0; i < str.size(); ++i) {
if (count[str[i]] == 0) {
result.push_back(str[i]);
}
}
return result;
}
void test(const std::string& str, const std::string& mask)
{
std::cout << "String : " << str << "\nmask string : " << mask << "\n";
std::cout << "After removing masked charcters : " <<
removeMaskedCharacters(str, mask) << "\n";
}
int main()
{
std::string str = "Hello World!! This is a C++ program";
std::string mask = "odsam";
test(str, mask);
return 0;
}