0

I'm trying to figure out a decent way to replace spaces (\x20) that occur next to each other with &nbps;'s.

string SPACE = "\x20";
string str = "<span style=\"color:#f00;\"">   Three Spaces</span>";
size_t pos = str.find(SPACE, 0);
size_t prev = 0;
size_t start = 0;
size_t length = 0;

while (pos != string::npos)
{
    prev = pos;
    if (prev == (start + 1))
    {
        // this is where i start to get lost...
    }
    else
    {
        start = pos;
    }
    pos = str.find(SPACE, pos + 1);
}

So my end result of the str would be

<span style="color:#f00;">&nbsp;&nbsp;&nbsp;Three Spaces</span>

Notice that the space within the span is kept the same.

I'm having a rough time thinking of the logic to keep track of how many spaces are next to each other. I was going to find the start position, store a "length" of how many spaces it finds, then do a substr() with the pos and length.

Onyxdragun
  • 117
  • 1
  • 3
  • 13
  • What is your question? Other than "how do I write my program", that is, which is off-topic – Lightness Races in Orbit May 26 '14 at 18:20
  • I guess, I'm looking to make sure I am on the right path. Find the first space, then check if the next space is +1 from the previous then if it is, make length++; – Onyxdragun May 26 '14 at 18:23
  • 1
    Try with two passes. first replace all double-spaces by `  `. Then replace all `  ` by `  `. However make sure you don't have double-spaces in your code. – agrum May 26 '14 at 18:24
  • This isn't really what SO is for. – Lightness Races in Orbit May 26 '14 at 18:25
  • Your first task should be finding where the element body *starts*, as you have pointed out yourself your algorithm need not replace spaces in the span element *tag* or attribute list (the latter of which I question), but rather in its body. That would seem to be the first thing you should tackle, and I'll warn you, it isn't as trivial as it may seem (and perhaps that is the crux of this question to begin with). – WhozCraig May 26 '14 at 18:27
  • SO isn't about asking to help someone with some logic/algorithm help? Though I've seen countless times where people are like. I have this problem solve it for me.. oh and show me the code. I guess I'm more looking for a little brain nudge in the right direction or if anyone else might have an easier way of doing it. *shrug* – Onyxdragun May 26 '14 at 18:28
  • 2
    Start with this: http://stackoverflow.com/a/8362145/560648 – Lightness Races in Orbit May 26 '14 at 18:28
  • 1
    @Onyxdragun: No it's not, really. That's more of a "discussion" and this is a Q&A repository. Each post should be useful to multiple people, not just the person posting them. This one is not sufficiently concrete for my tastes. Regardless, because I'm nice, I've given you a hint above. – Lightness Races in Orbit May 26 '14 at 18:29
  • Or just have a counter for spaces and replace the characters once a non-space character is found when the count is greater than 1? –  May 26 '14 at 20:33

2 Answers2

1

You can use a while loop to replace every occurrence of \x20\x20 with &nbsp;&nbsp; until there is nothing left to replace. IIRC HTML respects single spaces next to a &nbsp;, so you shouldn't need to worry about odd-numbered groups of spaces. If you really need every cluster of spaces to be replaced, you can replace &nbsp;\x20 with &nbsp;&nbsp; after you've replaced all double-spaces.

Take a peek at this example: https://stackoverflow.com/a/5343250/1127098

Community
  • 1
  • 1
xikkub
  • 1,641
  • 1
  • 16
  • 28
1
string SPACE = "\x20";
string str = "<span style=\"color:#f00;\">   Three Spaces</span>";
size_t pos = str.find(SPACE, 0);
string replace_str="&nbps;";

while (pos != string::npos)
{
    size_t number_of_space=1;
    for(size_t i=pos+1;i<str.size();i++)
    {
        if(str[i]=='\x20'){
            number_of_space++;
        }
        else{
            break;
        }
    }
    if(number_of_space>1)
    {
        while(number_of_space>0){
            str.erase(str.begin()+pos);
            str.insert(pos,replace_str);
            number_of_space--;
            pos+=replace_str.size();
        }
    }
    pos = str.find(SPACE, pos + 1);
}
uchar
  • 2,552
  • 4
  • 29
  • 50