From 2aefb62e311f7873fa31ee7bfc0de92eadd89c47 Mon Sep 17 00:00:00 2001 From: Aditi Chaurasia <103958459+aditi-chaurasia@users.noreply.github.com> Date: Thu, 20 Oct 2022 00:47:44 +0530 Subject: [PATCH] Create RemoveChar.cpp Hacktoberfest Submission. #13 --- Cpp/RemoveChar.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Cpp/RemoveChar.cpp diff --git a/Cpp/RemoveChar.cpp b/Cpp/RemoveChar.cpp new file mode 100644 index 0000000..19f978b --- /dev/null +++ b/Cpp/RemoveChar.cpp @@ -0,0 +1,60 @@ +// C++ program to remove duplicates, the order of +// characters is not maintained in this progress +#include +#define NO_OF_CHAR 256 +using namespace std; + +int* getcountarray(string str2) +{ + int* count = (int*)calloc(sizeof(int), NO_OF_CHAR); + + for (int i = 0; i < str2.size(); i++) + { + count[str2[i]]++; + } + + return count; +} + +/* removeDirtyChars takes two +string as arguments: First +string (str1) is the one from +where function removes dirty +characters. Second string(str2) +is the string which contain +all dirty characters which need +to be removed from first +string */ +string removeDirtyChars(string str1, string str2) +{ + // str2 is the string + // which is to be removed + int* count = getcountarray(str2); + string res; + + // ip_idx helps to keep + // track of the first string + int ip_idx = 0; + + while (ip_idx < str1.size()) + { + char temp = str1[ip_idx]; + if (count[temp] == 0) + { + res.push_back(temp); + } + ip_idx++; + } + + return res; +} + +// Driver Code +int main() +{ + string str1 = "geeksforgeeks"; + string str2 = "mask"; + + // Function call + cout << removeDirtyChars(str1, str2) << endl; +}