let's say I have the following line:
string s = "A B C D TESTTEST BLA BLA TEST TEST2"
Then I need the following:
"A B C D TESTTEST BLA BLA TEST TEST2"
So, the rules:
a) replace every second space ( between non-spatial characters ) with
b) If the last place is replaced by , try moving it back (if possible) one step so that the next word does not touch the force space.
Background:
I want to use this to print data from a database on my website. But I want to optimize push spaces to use less space. I also want the last forced space not to touch the next word (if possible), so it’s easier for some search engines to catch that word.
Do I need to iterate over each char in this line and count this event, or is there an easier, faster and more bizarre way?
Thanks, both solutions work perfectly.
So I made a benchmark to figure out which decision to make:
@Guffa, your solution takes 22 seconds for 1 million runs
@Timwi, your solution takes 7 seconds for 1 million runs
I will give both of you, but I will decide Timwi.