12345678910111213141516171819202122232425262728293031 |
- /* Create two functions toCamelCase() and toSnakeCase() that each take
- * a single string and convert it into either camelCase or snake_case.
- * If you're not sure what these terms mean, check the Resources tab above.
- */
- // rule 1, check for capitals
- // rule 2, check for underscore
- std::string toSnakeCase(std::string str) {
- for(int i = 0;i<str.size();i++){
- // insert an underscore before the uppercase
- if(isupper(str[i])){
- // set uppercase to lowercase
- str[i]=tolower(str[i]);
- str.insert(str.begin()+i,'_');
- }
- }
- return str;
- }
- std::string toCamelCase(std::string str) {
- int p;
- // find underscore in string
- while(std::string::npos != (p = str.find('_'))){
- // capitalize character after underscore
- if(str.size() != (p+1)){
- str[p+1]=toupper(str[p+1]);
- }
- // remove underscore
- str.erase(p,1);
- }
- return str;
- }
|