# Convert string to camel case [6 kyu] [Convert string to camel case](https://www.codewars.com/kata/517abf86da9663f1d2000003) 6 kyu ## Solution ```cpp= /** *** Author: R-CO *** E-Mail: daniel1820kobe@gmail.com *** Date: 2020-08-05 **/ #include <cctype> #include <cstdlib> #include <iostream> using std::cout; using std::endl; #include <string> struct TestCaseStruct { std::string input; std::string expected_output; }; class Solution { public: std::string to_camel_case(std::string text) { std::string output; bool to_upper = false; for (auto &ch : text) { if (ch == '_' || ch == '-') { to_upper = true; } else { if (to_upper) { output += toupper(ch); to_upper = false; } else { output += ch; } } } return output; } }; int main(int argc, char *argv[]) { /* to_camel_case("the-stealth-warrior") // returns "theStealthWarrior" to_camel_case("The_Stealth_Warrior") // returns "TheStealthWarrior" */ TestCaseStruct test_cases[] = {{"the-stealth-warrior", "theStealthWarrior"}, {"The_Stealth_Warrior", "TheStealthWarrior"} }; Solution solution; for (const auto& test_case : test_cases) { if (solution.to_camel_case(test_case.input) == test_case.expected_output) { cout << "test case \"" << test_case.input << "\" is pass." << std::endl; } else { cout << "test case \"" << test_case.input << "\" is fail." << std::endl; } } return EXIT_SUCCESS; } ``` ## Result  ###### tags: `CodeWars` `C++`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up