How to remove all the occurrences of a char in c++ string

remove all occurrences of a character in a string java
remove all occurrences of a character in a string c
remove all occurrences of a character in a string python
remove character from string c
remove character from string c++
remove all occurrences of a particular character from the string
c program to remove special characters from string
c program to delete a substring from a string

I am using following:

replace (str1.begin(), str1.end(), 'a' , '')

But this is giving compilation error.

Basically, replace replaces a character with another and '' is not a character. What you're looking for is erase.

See this question which answers the same problem. In your case:

#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

Or use boost if that's an option for you, like:

#include <boost/algorithm/string.hpp>
boost::erase_all(str, "a");

All of this is well-documented on reference websites. But if you didn't know of these functions, you could easily do this kind of things by hand:

std::string output;
output.reserve(str.size()); // optional, avoids buffer reallocations in the loop
for(size_t i = 0; i < str.size(); ++i)
  if(str[i] != 'a') output += str[i];

C Program to Remove All Occurrences of a Character in a String, from user, store it in some variable say toRemove. Using Function. The main () calls the deletechar (char *s, char c) function by passing the string, character as arguments to the function. 2) The function deletechar (char *s, char c) will remove all occurrences of the entered character from the string.

The algorithm std::replace works per element on a given sequence (so it replaces elements with different elements, and can not replace it with nothing). But there is no empty character. If you want to remove elements from a sequence, the following elements have to be moved, and std::replace doesn't work like this.

You can try to use std::remove (together with std::erase) to achieve this.

str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

Remove all occurrences of a character in a string, This program allows the user to enter a string (or character array), and a character value. Next, this C Program will find and remove all occurrences of a character� Input string from user, store in some variable say str. Input character to remove from user, store it in some variable say toRemove. Run a loop from start character of str to end. Inside the loop, check if current character of string str is equal to toRemove.

Using copy_if:

#include <string>
#include <iostream>
#include <algorithm>
int main() {
    std::string s1 = "a1a2b3c4a5";
    char s2[256];
    std::copy_if(s1.begin(), s1.end(), s2, [](char c){return c!='a';});
    std::cout << s2 << std::endl;
    return 0;
}

How to remove all the occurrences of a char in c++ string, Write a program to remove all the occurrences of a character in the string. Examples: Input : s = "geeksforgeeks" c = 'e' Output : s = "gksforgks"� If matched then replace the matched elements with ‘*’.Then return ‘*’ to main (). Repeat this step for all elements of the string. 2) The main () calls the deleteduplicate (char *s, char c)by passing the string and the duplicate character as arguments to the function to delete the repeated elements from the string.

string RemoveChar(string str, char c) 
{
   string result;
   for (size_t i = 0; i < str.size(); i++) 
   {
          char currentChar = str[i];
          if (currentChar != c)
              result += currentChar;
   }
       return result;
}

This is how I did it.

Or you could do as Antoine mentioned:

See this question which answers the same problem. In your case:

#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

To Delete all occurrences of Character from the String, Basically, replace replaces a character with another and '' is not a character. What you're looking for is erase . See this question which answers the same� C program to remove all occurrences of a character from the entered string using a while a loop.| सी प्रोग्राम एक लूप का उपयोग करते हुए दर्ज स्ट्रिंग से एक चरित्र की सभी घटनाओं को हटाने के लिए।

This code removes repetition of charecters i.e, if the input is aaabbcc then the output will be abc.

cin >> s;
ans = "";
ans += s[0];
for(int i = 1;i < s.length();++i)
if(s[i] != s[i-1])
    ans += s[i];
cout << ans << endl;

C Program To Remove All Occurrences Of A Character From String , be displayed as output except given Character. C Program: #include #include #include void del(char […] To Delete all occurrences of Character from the String. But there is no empty character. If you want to remove elements from a sequence, the following elements have to be moved, and std::replace doesn't work like this. You can try to use std::remove (together with std::erase) to achieve this. str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

Program to remove all occurrences of a character from string|delete , 2) The function deletechar(char *s, char c) will remove all occurrences of the entered character from the string. a) for loop iterates through the string with the� Remove all occurrences of a character in a string Recursive program to replace all occurrences of pi with 3.14 in a given string Map every character of one string to another such that all occurrences are mapped to the same character

C Program to Remove all Characters in a String Except Alphabets, About this video lecture: c program to remove all occurrences of a character from the string Duration: 14:21 Posted: Apr 8, 2018 Given a string, inplace remove all occurrences of "AB" and "C" from it. The main challenge lies in the in-place doing the conversion in one traversal of the string. The problem demands removal of all adjacent as well as non-adjacent occurrences of string AB i.e. for a string for a given string say ADAABCB

In-place remove all occurrences of 'AB' and 'C' from the string , In this C programming example, you will learn to remove all the characters from a string entered by the user except the alphabets. Write a program to remove all the occurrences of a character in the string. Examples: Input : s = "geeksforgeeks" c = 'e' Output : s = "gksforgks" Input : s = "geeksforgeeks" c = 'g' Output : s = "eeksforeeks"

Comments
  • '' is not a character indeed.
  • Well, it would certainly help to know the error you're getting.
  • be nice, there are lots of contexts where replace is an appropriate thought, just not thisone.
  • possible duplicate of How to remove characters from a string
  • Isn't the algorithm you provided O(n^2)?
  • @jww: I assume you're talking about the last code sample and n is the original string length. For each input character, I do 1 character test O(1), and 0 or 1 character append. Character append is O(1) is enough memory is reserved, or O(current_length) if a new buffer is allocated. If you do output.reserve(str.size()) before the loop this never happens and you have a global O(n) cost. Otherwise asymptotically, I guess the cost is O(n . log(n) ) because of STL container reallocation strategy.
  • I needed #include <algorithm>
  • Nice answer. It's always good if the answer contains many solutions. For me, the solution with the for is the most suitable.
  • @DmitryNichiporenko the answer with the for cannot be the most suitable. If you have a predicate or a non empty output I would rather consider: output.reserve(str.size()+output.size()); std::copy_if(str.begin(), str.end(), std::back_inserter(output), [](char c){return predicate(c);});
  • You guess right. Rather than writing your own, better find out why you can't use standard C++ headers.
  • Well that's a personal opinion xtofl, It's not always good to use 3rd code you actually don't know what it does nor the performances rather than writing what you specifically need.
  • I understand what you mean. It's humility though that makes me choose for the version that's been reviewed, tested, optimized by professional full time library writers, rather than my own. The standard library can be regarded as required knowledge: its functions as well as its runtime complexity.
  • Strings aside, It's a C solution to a C++ problem. I don't think this should have been voted down.
  • Looking back to this question 4 months after, I really don't know why I didn't use std::erase or std::replace.
  • yeah, I think there is an implied loop over hte array chars he left out