How to Remove All the Occurrences of a Char in C++ String

How to remove all the occurrences of a char in c++ 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 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];

How to remove all occurrences of a given character from string in C?

You can do it like this:

void remove_all_chars(char* str, char c) {
char *pr = str, *pw = str;
while (*pr) {
*pw = *pr++;
pw += (*pw != c);
}
*pw = '\0';
}

int main() {
char str[] = "llHello, world!ll";
remove_all_chars(str, 'l');
printf("'%s'\n", str);
return 0;
}

The idea is to keep a separate read and write pointers (pr for reading and pw for writing), always advance the reading pointer, and advance the writing pointer only when it's not pointing to a given character.

Removing all occurrences of a character from a string in C++

If I have understood the description of the assignment correctly,
then you need something like the following:

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>

void removeChar( std::string::size_type &n, std::string &str, const std::string &s, char c )
{
str.clear();

c = std::toupper( ( unsigned char )c );

auto equal_to_c = [&c]( const auto &item )
{
return std::toupper( ( unsigned char )item ) == c;
};

std::remove_copy_if( std::begin( s ), std::end( s ),
std::back_inserter( str ),
equal_to_c );

n = s.size() - str.size();
}

int main()
{
std::string::size_type n = 0;
std::string str;

removeChar( n, str, "This is a silly assignment", 's' );

std::cout << "n = " << n << ", str = " << str << '\n';

return 0;
}

The program output is:

n = 5, str = Thi i a illy aignment

remove all occurences of a character in C string - Example needed

for (s=d=str;*d=*s;d+=(*s++!='"'));

How to remove certain characters from a string in C++?

   string str("(555) 555-5555");

char chars[] = "()-";

for (unsigned int i = 0; i < strlen(chars); ++i)
{
// you need include <algorithm> to use general algorithms like std::remove()
str.erase (std::remove(str.begin(), str.end(), chars[i]), str.end());
}

// output: 555 5555555
cout << str << endl;

To use as function:

void removeCharsFromString( string &str, char* charsToRemove ) {
for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
}
}
//example of usage:
removeCharsFromString( str, "()-" );

removing all sub strings of a string in C

Your code has multiple issues:

  • the loop for (int i = 0; i <= strlen(str); i++) is very inefficient as the length of str is recomputed at each iteration of the loop. You should simply write:

    for (int i = 0; str[i] != '\0'; i++)
  • when you skip the matched substring, the character after it is copied and i is incremented in the loop so the next occurrence is not tested correctly.

  • the code to append a single character is inefficient: instead of strncat(new, &ch, 1); you should have a pointer to the end of the destination string and write *p++ = str[i]; and set the null terminator after the end of the loop.

Here is a modified version:

#include <string.h>
// remove all occurrences of sub in str into destination buffer new
// return a pointer to the destination string
char *removeSub(cont char *str, const char *sub, char *new) {
char *p = new;
size_t len = strlen(sub);
if (len > 0) {
const char *subp;
while ((subp = strstr(str, sub)) != NULL) {
memcpy(p, str, sub - str);
p += sub - str;
str = sub + len;
}
}
strcpy(p, str);
return new;
}

And an alternative without library calls:

// remove all occurrences of sub in str into destination buffer new
// return a pointer to the destination string
char *removeSub(cont char *str, const char *sub, char *new) {
char *p = new;
while (*str) {
if (*str == *sub) { /* potential substring match */
for (size_t i = 1;; i++) {
if (sub[i] == '\0') { /* substring match */
str += i;
break;
}
if (str[i] != sub[i]) {
*p++ = *str++;
break;
}
}
} else {
*p++ = *str++;
}
}
*p = '\0';
return new;
}

Remove all instances of a character from string

You can use Regular Expression( here i have assumed that character is x):

string result = Regex.Replace( input , "x(?=\\S)" , "");

Live Demo. Please check that it uses very few steps finding it.



Related Topics



Leave a reply



Submit