← Back to Functions
Question 720
Trim Spaces From Both Ends
Trim Spaces From Both Ends
Write the functionstring trim(const string& text)so it returnstextwith every space and tab removed from the START and the END only. Spaces inside the text stay where they are, and a string that is nothing but spaces becomes the empty string.find_first_not_of(" \t")gives the index of the first real character (orstring::nposwhen there is none) andfind_last_not_of(" \t")the index of the last one;erasecan then cut from the front and from the back. The hidden driver reads every line of input withgetlineand prints each trimmed result between square brackets on its own line, so for the three linesRed Kite, a tab-wrappedPaper BoatandNo Changeit prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
[Red Kite] [Paper Boat] [No Change]
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Functions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.