You are viewing a single comment's thread. Return to all comments →
What is wrong with this code, and why didn't it pass the hidden test cases?
#include <string> #include <sstream> #include <cctype> #include <iostream> using namespace std; string splitcase(string str, char type) { string result; for (char c : str) { if (isupper(c) && !result.empty()) { result += " "; } result += tolower(c); } if (type == 'M') { result.erase(result.end() - 2, result.end()); } return result; } string combinedcase(string str, char type) { stringstream ss(str); string word, result; bool iffirstword = true; while (ss >> word) { if (iffirstword) { if (type == 'C') { word[0] = toupper(word[0]); } else { word[0] = tolower(word[0]); } iffirstword = false; } else { word[0] = toupper(word[0]); } result += word; } if (type == 'M') { result += "()"; } return result; } int main() { string input; while (getline(cin, input)) { char operation = input[0]; char type = input[2]; string data = input.substr(4); if (operation == 'C') { cout << combinedcase(data, type) << endl; } if (operation == 'S') { cout << splitcase(data, type) << endl; } } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Camel Case 4
You are viewing a single comment's thread. Return to all comments →
What is wrong with this code, and why didn't it pass the hidden test cases?