Skip to content

Instantly share code, notes, and snippets.

@gitaaron
Created February 26, 2019 23:29
Show Gist options
  • Save gitaaron/8cfce0c030f1fba7d2ebf9dfbe0ecea8 to your computer and use it in GitHub Desktop.
Save gitaaron/8cfce0c030f1fba7d2ebf9dfbe0ecea8 to your computer and use it in GitHub Desktop.
#include <iostream>
std::string rtrim(char *from, size_t len) {
char to[len+1];
strncpy(to, from, len);
while(len > 0) {
if(to[len-1] == ' ' || to[len-1] == '\0') {
len--;
} else {
break;
}
}
to[len] = '\0';
return std::string(to);
}
int main() {
char first[] = "first";
char second[] = "secondsecond";
std::cout << "rtrim: " << rtrim(first, 5) << std::endl;
std::cout << "rtrim: " << rtrim(second, 12) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment