This C++ Program which removes a given character from the string. The program takes a string and the character to be removed as the input, uses standard library remove( ) algorithm for returning a range of elements with all the elements equal to the character removed which when passed to the standard library algorithm erase( ) erases the character from the entire string.
Here is source code of the C++ program which removes a given character from the string. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Remove given Character from the String
*/
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
char c;
std::cout << "Enter the string : ";
std::cin >> s;
std::cout << "\nEnter the character : ";
std::cin >> c;
/* Removing character c from s */
s.erase(std::remove(s.begin(), s.end(), c), s.end());
std::cout << "\nString after removing the character "
<< c << " : " << s;
}
$ g++ main.cpp $ ./a.out Enter the string : Mediterranzean Enter the character : z String after removing the character z : Mediterranean
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C++ Books
- Apply for C++ Internship
- Check Programming Books