Last active
November 22, 2023 19:34
-
-
Save juanfal/b9fae927922b3c426f5c7a32a7655b2a to your computer and use it in GitHub Desktop.
break_camel_case
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
// t11e12.breakCamelCase.cpp | |
// juanfc 2023-11-22 | |
// https://gist.github.com/b9fae927922b3c426f5c7a32a7655b2a | |
#include <iostream> | |
using namespace std; | |
// proto | |
string breakCamelCase(string s); | |
int main() | |
{ | |
cout << breakCamelCase("camelCasing") << endl; | |
cout << breakCamelCase("__A__aA_A_") << endl; | |
cout << breakCamelCase("camelC") << endl; | |
cout << breakCamelCase("camElC") << endl; | |
cout << breakCamelCase("APPLE") << endl; | |
return 0; | |
} | |
// functions | |
bool isUpperCase(char c); | |
string breakCamelCase(string s) | |
{ | |
string r; | |
for (int i = 0; i < s.length(); i++) { | |
if (isUpperCase(s[i])) { | |
if (i > 0 and s[i-1] != '_') | |
r += '_'; | |
r += (s[i] + ('a'-'A')); | |
} else | |
r += s[i]; | |
} | |
return r; | |
} | |
bool isUpperCase(char c) | |
{ | |
return (c >= 'A' and c <= 'Z'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment