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
parent
f77d5e8e3a
commit
36204a35bb
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
*/
|
Loading…
Reference in New Issue