2

Here's what I have so far:

string stripSymbols(string str) {
    int stringSize = strlen(str.c_str());

    for (int i = 0; i < stringSize; i++)
        if (str[i] == 0x46)
            str[i] = 0x32;
    return str;
}

I know the ascii codes are probably wrong. That's part of the problem. But once I figure those out, I was thinking I could maybe put a switch in here for each symbol that would replace each symbol with an empty space.

Or better yet, I could have a for loop in my for loop that loops through a string of symbols and replaces any that match with the user's input with an empty space.

I have a couple ideas, but I was wondering if there was a more efficient way of doing this.

Update 1:

This code looks a little better and works:

string stripSymbols(string str) {
    int stringSize = str.size();
    for (int i = 0; i < stringSize; i++)
        if (str[i] == '.')
            str[i] = ' ';
    return str;
}

But the replies offer a more efficient solution.

Update 2:

Solution inspired by Kerrek SB's reply:

char symbols [] = {'!', '?', ',', '\'', '.'};
int symbols_size = sizeof(symbols) / sizeof(char);  

for (int j = 0; j < symbols_size; j++)
    replace(str.begin(), str.end(), symbols[j], ' ');
subtlearray
  • 1,251
  • 1
  • 11
  • 23
  • 1
    If you are not sure about the ASCII codes, why not use literal chars instead of using numerical codes and hoping they're correct? – Mr Lister Feb 16 '12 at 19:21
  • 1
    Jeez... who gave you the idea to write `strlen(str.c_str())`??? How about `str.size()`? – Kerrek SB Feb 16 '12 at 19:23
  • I agree with previous comments, I will just add: You could pass `str` by reference and not return anything. From what I understand you were going to use your function like this: `str = stripSymbols(str);`. – LihO Feb 16 '12 at 19:26
  • 1
    By the way, `0x46 == 'F'` and `0x32 == '2'`. Do you intend to replace all each "F" with a "2"? – Robᵩ Feb 16 '12 at 19:30
  • 1
    You probably want decimal 46 (`'.'`) and 32 (`' '`); using character literals instead of magic numbers makes it much easier both to read and to write. – Mike Seymour Feb 16 '12 at 19:38
  • @KerrekSB Fixed. I have no experience with dealing with string operations in C++, so bare with me... – subtlearray Feb 16 '12 at 19:46
  • @Rob I wanted to replace a period with a space. And Mike, your suggestion of using char literals helped. I forgot there's a subtle difference between defining a string and char in C++. I was using " " which resulted in error. – subtlearray Feb 16 '12 at 19:49

5 Answers5

3

If you want to replace any non-alphanumeric character with a space:

std::replace_if(str.begin(), str.end(), [](char c){return !std::isalnum(c);}, ' ');
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

Take a look at string::find and string::replace methods.

Archie
  • 6,391
  • 4
  • 36
  • 44
2

If I were going for a robust solution I'd probably just use boost::regex_replace:

// regex with illegal characters
std::string output = boost::regex_replace(str, "[!@#%]", " ");

Documentation

Ron Warholic
  • 9,994
  • 31
  • 47
2

I'd try something like this:

std::replace(str.begin(), str.end(), 0x46, 0x32);

You can do this in the original context even, no need for a separate function. You need to #include <algorithm>.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

Are you wishing to just remove/replace every instance of a specific character from a std::string? If so, see these similar questions:

How to replace all occurrences of a character in string?

Remove spaces from std::string in C++

And just use the concepts displayed in the answers to suit your own goals.

Community
  • 1
  • 1
ssell
  • 6,429
  • 2
  • 34
  • 49