• Register
100 points
7 2
Welcome to Kodlogs, programming questions and answer website.

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

Code

#include <iostream> 
#include <string> 
using namespace std; 
  

void funcErase(string str) 
{ 
    // Deletes character at position 4 
    str.erase(str.begin() + 4); 
  
    cout << "After erase : "; 
    cout << str; 
} 
  

int main() 
{ 
    string str("Hello World!"); 
  
    cout << "Before erase : "; 
    cout << str << endl; 
    funcErase(str); 
  
    return 0; 
} 

Output

Before erase : Hello World!
After erase : Hell World!

 
100 points
7 2