Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
break_camel_case
// t10e12.breakCamelCase.cpp
// juanfc 2022-01-14
//
#include <iostream>
using namespace std;
// proto
string breakCamelCase(string s);
int main()
{
cout << breakCamelCase("camelCasing") << 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)
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