How to remove a particular character from a string in C++

This tutorial aims at a string operation which is to remove a particular character from the string in C++ as desired by the user. The users may need to use this concept to correct the mistakes in the entered data or remove unwanted characters from the data. Feel free to leave a comment or ask any questions in the comments section at the end of the post.

C++ Program to remove a particular character from a string

Let’s go through the program step by step

1. Libraries

Code :

#include <iostream>
#include <string>
#include <algorithm>
  1. iostream : iostream library provides basic input and output services for C++ programs using objects like ‘cin’ and ‘cout’ in our case.
  2. string : string library is included to use the various string manipulation functions as we are dealing with string operations.
  3. algorithm : algorithm library provides a collection of functions that are designed to be used on a range of elements that are specified using start and end of the range. Searching, sorting, manipulation are some of the various uses of the functions in this library.

 

2. Declaring the variables

Code :

string main_string;
char delete_char;
  1. main_string : This variable is of data-type string. It is used to store the string from which we need to delete a character.
  2. delete_char : This variable is of data-type char. It is used to store the character which the user wants to delete.

 

3. Input data

Code :

cout << "\nEnter the main string : ";
cin >> main_string;

cout << "\nEnter the character to be deleted : ";
cin >> delete_char;

Now we ask the user to enter the main string and then the specific character from the string which the user wants to delete.

Output :

Enter the main string : codezspeedy

Enter the character to be deleted : z

4. Delete the character from the string

Code :

main_string.erase(   remove(main_string.begin(), main_string.end(), delete_char) ,    main_string.end()   );

Let’s break down this statement and understand the operation.

erase() : This function by default erases all the characters in the string. If we specify the start and end of the strings, we can delete the particular range of characters between the range.

How to remove a particular character from a string in C++

erase() function has two parameters :

A.    remove() function: This function is used to remove the specific character from the string.
remove() function has three parameters.

  1. main_string.begin() : The begin() function is used to specify the start of the string in consideration
  2. main_string.end(): The end() function is used to specify the end of the string in consideration.
  3. delete_char : The third parameter is the variable(char) that contains the character that is supposed to be deleted.

B.     main_string.end() : The erase() function requires a second parameter specifying the end of the string.

(Note: This command erases/removes all the occurrences of the character which is entered by the user to be deleted from the main_string. )

 

5. Displaying the result

cout << "\nString after deleting the character (" << delete_char << ") : \n" << main_string << "\n\n";

Our main_string has been operated on and the character entered by the user is deleted from the main_string. We display the main_string, using cout function, to the user.

( Note: The “\n” is used to enter a new line for better presentation. You can skip it if you find that more suitable. )

Output :

String after deleting the character (z) :
codespeedy

 

Complete Code :

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string main_string;
    char delete_char;

    cout << "\nEnter the main string : ";
    cin >> main_string;
    cout << "\nEnter the character to be deleted : ";
    cin >> delete_char;

    main_string.erase(   remove(main_string.begin(), main_string.end(), delete_char) ,    main_string.end()   );

    cout << "\nString after deleting the character (" << delete_char << ") : \n" << main_string << "\n\n";

}

Output :

Enter the main string : codezspeedy

Enter the character to be deleted : z

String after deleting the character (z) :
codespeedy

 

You may also learn,

How to Add A Character To A String in C++

How To Find The Length Of A String In C++

Leave a Reply

Your email address will not be published. Required fields are marked *