is there any way to both the cases in single while loop leading and trailing cases? |
Yes, but that requires more attention.
You need a start_pos. Set it to 0.
Search for the backslash only using start_pos (as seeplus showed).
Set bool found_space = false;
Within the loop check whether theres is a trailing (pos + 1) space. If so remove it, set found_space = true. Note that before checking the space you need to check whether the backslash isn't the
last character.
Similar for the leading backspace:
Within the loop check whether theres is a leading (pos - 1) space. If so remove it, set found_space = true. Note that before checking the space you need to check whether the backslash isn't the
first character.
When found_space == false set start_pos = pos + 1;
This is a good execise for you to check your understanding of the subject...