Remove all occurrences of a character from a string using STL Last Updated : 21 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string S and a character C, the task is to remove all the occurrences of the character C from the given string. Examples: Input:vS = "GFG IS FUN", C = 'F' Output:GG IS UN Explanation: Removing all occurrences of the character 'F' modifies S to "GG IS UN". Therefore, the required output is GG IS UN Input: S = "PLEASE REMOVE THE SPACES", C = ' ' Output: PLEASEREMOVETHESPACES Explanation: Removing all occurrences of the character ' ' modifies S to "GG IS UN". Approach: The idea is to use erase() method and remove() function from C++ STL. Below is the syntax to remove all the occurrences of a character from a string. S.erase(remove(S.begin(), S.end(), c), S.end()) Below is the implementation of the above approach: C++ // C++ program of the above approach #include <algorithm> #include <iostream> using namespace std; // Function to remove all occurrences // of C from the string S string removeCharacters(string S, char c) { S.erase(remove( S.begin(), S.end(), c), S.end()); return S; } // Driver Code int main() { // Given String string S = "GFG is Fun"; char C = 'F'; cout << "String Before: " << S << endl; // Function call S = removeCharacters(S, C); cout << "String After: " << S << endl; return 0; } Output: String Before: GFG is Fun String After: GG is un Time Complexity: O(N2)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Remove duplicates from a string using STL in C++ R rohit5swd Follow Improve Article Tags : Strings DSA STL cpp-strings-library Practice Tags : STLStrings Similar Reads Remove duplicates from a string using STL in C++ Given a string S, remove duplicates in this string using STL in C++ Examples: Input: Geeks for geeks Output: Gefgkors Input: aaaaabbbbbb Output: ab Approach: The consecutive duplicates of the string can be removed using the unique() function provided in STL. Below is the implementation of the above 1 min read Remove all occurrences of a string t in string s using Boyer-Moore Algorithm Given a string s and string t, the task is to remove all occurrences of a string t in a string s using the Boyer-Moore algorithm. Examples: Input: s = "ababaababa", t = "aba" Output: baab Input: s = "Geeksforgeeks", t = "eek"Output: Gsforgs Approach: This can be solved with the following idea: We in 10 min read Remove all duplicate adjacent characters from a string using Stack Given a string, str, the task is to remove all the duplicate adjacent characters from the given string. Examples: Input: str= âazxxzyâOutput: ay Removal of "xx" modifies the string to âazzyâ. Now, the removal of "zz" modifies the string to âayâ. Since the string âayâ doesn't contain duplicates, the 6 min read Remove all occurrences of string t in string s using KMP Algorithm Given two strings s and t, the task is to remove all occurrences of t in s and return the modified string s, and you have to use the KMP algorithm to solve this. Examples: Input: s = "abcdefgabcabcabdefghabc", t = "abc"Output: "defgdefgh" Input: s = "aaabbbccc", t = "bbb"Output: "aaaccc" Approach: T 8 min read Remove a character from a string to make it a palindrome Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this. Examples: Input : str = âabcbaâ Output : Yes we can remove character âcâ to make string palindrome Input : str = âabcbeaâ Output : Yes we can remove character âeâ 10 min read Remove all characters other than alphabets from string Given a string consisting of alphabets and others characters, remove all the characters other than alphabets and print the string so formed. Examples: Input : $Gee*k;s..fo, r'Ge^eks?Output : GeeksforGeeks Input : P&ra+$BHa;;t*ku, ma$r@@s#in}ghOutput : PraBHatkumarsingh Recommended PracticeRemove 12 min read Like