From 36204a35bb46edffe8cb80c1c0cc1dfe13b3314c Mon Sep 17 00:00:00 2001 From: Samruddhi Ghodake <72791227+samughodake@users.noreply.github.com> Date: Wed, 22 Sep 2021 19:47:57 +0530 Subject: [PATCH] chore(CPlusPlus): add remove occurence of characters of 2nd string from the 1st string (#475) Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 1 + .../CPlusPlus/Strings/remove-occurrences.cpp | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 algorithms/CPlusPlus/Strings/remove-occurrences.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index bb4a3c17..186abdde 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -93,6 +93,7 @@ 3. [String reversal](Strings/string-reverse.cpp) 4. [String tokanisation](Strings/string-tokeniser.cpp) 5. [Anagram check](Strings/anagram.cpp) +6. [Remove occurrences from string](algorithms/CPlusPlus/Strings/remove-occurrences.cpp) ## Trees diff --git a/algorithms/CPlusPlus/Strings/remove-occurrences.cpp b/algorithms/CPlusPlus/Strings/remove-occurrences.cpp new file mode 100644 index 00000000..6a79122b --- /dev/null +++ b/algorithms/CPlusPlus/Strings/remove-occurrences.cpp @@ -0,0 +1,48 @@ +/* +Description: A C++ program to remove occurrence of characters of 2nd string from the 1st string + +*/ + +#include +#include +using namespace std; + +string removeChars(string s1, string s2) { + + string out; + //checking if the character of 2nd string is present in the 1st string + //if not present, appending the character in the output string + for (char c : s1) { + + if (s2.find(c) == string::npos) { + + out += c; + } + } + return out; +} + +//main starts +int main() { + + string s1, s2; + cout << "Enter string 1: \n"; + getline(cin, s1); + cout << "Enter string 2: \n"; + getline(cin, s2); + string output = removeChars(s1, s2); + cout << output; + return 0; +} + +/* +Sample input +Enter string 1: +computer +Enter string 2: +cat + +output: +ompuer +(removed c,a,t) +*/