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>
pull/469/head^2
Samruddhi Ghodake 2021-09-22 19:47:57 +05:30 committed by GitHub
parent f77d5e8e3a
commit 36204a35bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -93,6 +93,7 @@
3. [String reversal](Strings/string-reverse.cpp) 3. [String reversal](Strings/string-reverse.cpp)
4. [String tokanisation](Strings/string-tokeniser.cpp) 4. [String tokanisation](Strings/string-tokeniser.cpp)
5. [Anagram check](Strings/anagram.cpp) 5. [Anagram check](Strings/anagram.cpp)
6. [Remove occurrences from string](algorithms/CPlusPlus/Strings/remove-occurrences.cpp)
## Trees ## Trees

View File

@ -0,0 +1,48 @@
/*
Description: A C++ program to remove occurrence of characters of 2nd string from the 1st string
*/
#include <string>
#include <iostream>
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)
*/