Diademata 2018. 2. 10. 13:36
반응형

Check if the given string is a correct time representation of the 24-hour clock.


Example


For time = "13:58", the output should be

validTime(time) = true;

For time = "25:51", the output should be

validTime(time) = false;

For time = "02:76", the output should be

validTime(time) = false.


code >>


bool validTime(std::string time) {

int index = time.find(":");

int hour = std::stoi(time.substr(0, index));

int min = std::stoi(time.substr(index + 1, 2));

if (hour >= 0 && hour <= 23 && min >= 0 && min <= 59)

return true;

else

return false;


}

반응형