Created
February 26, 2019 23:29
-
-
Save gitaaron/8cfce0c030f1fba7d2ebf9dfbe0ecea8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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