We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
string timeConversion(string s) {
string hour = s.substr(0, 2); // starts from 0 and len = 2
string amPM = s.substr(8); // from 0 to the rest
string result = "";
if (hour == "12") {
result += (amPM == "PM") ? "12" : "00";
} else {
result += (amPM == "PM") ? to_string(stoi(hour) + 12) : hour;
}
result += s.substr(2, 6); // starts from the index 2 and len = 6
return result;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
string timeConversion(string s) { string hour = s.substr(0, 2); // starts from 0 and len = 2 string amPM = s.substr(8); // from 0 to the rest
}