Skip to content

Instantly share code, notes, and snippets.

@rebelmachina
Created August 21, 2017 23:40
Show Gist options
  • Select an option

  • Save rebelmachina/24755679039acdf4d7cd96997966e1b2 to your computer and use it in GitHub Desktop.

Select an option

Save rebelmachina/24755679039acdf4d7cd96997966e1b2 to your computer and use it in GitHub Desktop.
strip surrounding spaces in C++
string stripSurroundingSpaces(string s) {
int i = 0;
while(i<s.size() && s[i] == ' ') {
i++;
}
int j = s.size()-1;
while(j >= 0 && s[j] == ' ') {
j--;
}
string res;
for(int k=i; k<=j; ++k)
res += s[k];
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment