Skip to content

Instantly share code, notes, and snippets.

@pallas
Created October 25, 2020 19:08
Show Gist options
  • Save pallas/8b8572fc0daa75ea312eafcf9dc3149e to your computer and use it in GitHub Desktop.
Save pallas/8b8572fc0daa75ea312eafcf9dc3149e to your computer and use it in GitHub Desktop.
in-place conversion of string from snake case to camel case
// SPDX-License-Identifier: Unlicense
// Author: Derrick Lyndon Pallas <[email protected]>
#include <stdbool.h>
#include <ctype.h>
char *
snake_to_camel(char * string) {
bool upper = true;
char * camel = string;
for (const char * snake = string ; *snake ; ++snake) {
if (isalpha(*snake)) {
*camel++ = upper ? toupper(*snake) : tolower(*snake);
} else if (isdigit(*snake)) {
*camel++ = *snake;
}
upper = !isalpha(*snake);
}
*camel = '\0';
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment