I want to know how to reduce the spaces between words if they are separated by more than one spaces.
I know a little bit of it.After Scanning the string to see if a space is encountered,what am i supposed to do?
Printable View
I want to know how to reduce the spaces between words if they are separated by more than one spaces.
I know a little bit of it.After Scanning the string to see if a space is encountered,what am i supposed to do?
You could use something like (pseudocode):
The simplest way to handle the removing is to just append the characters you want to keep to a new string:Code:bool last_character_was_a_space = false;
for-each-character {
if char == space {
if last_character_was_a_space {
remove_character;
} else {
keep_character;
last_character_was_a_space = true;
}
} else {
keep_character;
last_character_was_a_space = false;
}
}
Code:std::string result;
...
//keep_character
result += char;