Remove Character from String in C++

Remove character from String in C++

In this article, we are going to see how to remove character from the string in C++.

Remove a character from string without using library function in C++

We can remove a single character without using any library function. Following is the implementation. The character to be removed can have multiple occurrences.

Output:

Input string:
java2blog
Enter the character you want to remove
a
Converted string is: jv2blog

Remove multiple characters from string without using library function in C++

We can remove multiple characters without using any library function. Following is the implementation. Each character to be removed can have multiple occurrences.

Output:

Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm

std::remove() and string::erase() to remove character from string in C++

remove() is a library function which removes certain characters from the string. The usage can be seen in the following implementation. Actually it’s a function which removes certain value from a range. So the string is basically created into a range using iterator .begin() and .end(). The remove function returns the new end of the range, an iterator pointing to the last element not removed.
Erase the remaining part of the string, i.e., if it be the iterator returned from remove function then erase it to string.end()

Output:

Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm

std::remove_if() and string::erase() to remove character from string in C++

In the above solution, remove function is called for every character to be removed in the for loop. We can avoid that by using remove_if() function. It’s also a standard library function. In remove_if() we have an predicate function involved which returns true for characters to be removed. Implementation is following:

Output:

Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm

Obviously, this code is not straight forward as it was for remove function(). Here we haven’t used the for loop, we have lot iterated through all character to be removed, then how it is working?
remove_if() function has an argument predicate function. A predicate function is a Boolean function which is running for every elements in the range (str.begin(), str.end())
Our predicate is namely myfunc which is

rmv is declared as a global variable to be noted, so that in can be used in the predicate function. Now for every element c, we have checked whether it is part of the string which contains character to be removed or not. To do this, we have used string.find() function which returns string::npos if character not found. Based on this predicate function, the characters has been removed or unchanged.

That’s all about how to remove character from string in C++.

Related Posts

  • Read CSV file in C++
    01 November

    How to read CSV file in C++?

    Table of ContentsWhat is a CSV File? How To Read A CSV File In C++?C++ Code To Read A CSV FileHow To Read A CSV File Using C-Based Approach?Conclusion CSV files or Comma Separated Value Files are simple yet special files that we use to store data. Here, each value is separated by a comma, and […]

  • Count lines in file in C++
    01 November

    Count lines in file in C++

    Table of ContentsHow To Count Lines In A File In C++?C++ Program To Count Lines In A FileHow To Count Lines In A File In C++ Using C-style Approach?C++ Program to Count Lines In A File using C-style approachConclusion We often store our data into files when we need the data to be persistent and […]

  • Get Number of Elements in Array in C++
    08 October

    Get Number of Elements in Array in C++

    Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.