String is the sequence of characters as an object of class. String is like an array but with some exceptions. One of them is that if we decrease the size of the string by deleting or removing any character from the string, then it will deallocate the wasted memory.
Remove the character from a string
- Function for erasing the character
- Defining string to remove the character from the particular position
- Print the string before removal
- Call the function
- And print the string after removal of character
using namespace std;
void funcErase(string str)
// Deletes character at position 4
str.erase(str.begin() + 4);
cout << "After erase : ";
cout << str;
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
Before erase : Hello World!
After erase : Hell World!