Created
January 16, 2018 15:18
-
-
Save phrz/be90d3eaadf10cbc9f18b6be1b12b51c to your computer and use it in GitHub Desktop.
Reverses a string in C while minding buffer sizes.
This file contains hidden or 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
| void reverse(char* string, char* buffer, size_t buffer_limit) { | |
| if(buffer_limit < 1) { | |
| return; | |
| } | |
| size_t len = strnlen(string, buffer_limit); | |
| if(len == 0) { | |
| buffer[0] = '\0'; | |
| return; | |
| } | |
| buffer[len] = '\0'; | |
| for(size_t i = 0; i < len; i++) { | |
| buffer[len - i - 1] = string[i]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment