Hello,
let's say I have the following string:
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 are:
a) replace every second space (between non space chars) with
b) If the last space is replaced with
, try to move it backwards (if possible) one step so the next word is not touched by the force space.
Background:
I want to use that to print data from the database to my website. But I want to optimize the forced spaces to use less space. I also want the last forced space not to touch the next word (if possible) so it is easier for some search engines to catch that word.
Will I need to loop through every char in that string and count the occurrence or is there an easier, faster and more fancy way?
Thank you, both solutions are working perfect.
So I did a benchmark to figure out which solution to accept:
@Guffa, your solution needs 22 seconds for 1 Million runs
@Timwi, your solution needs 7 seconds for 1 Million runs
I will give you both an upvote, but I will accept Timwi's solution.