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.
Extract the A or P from the given time string. A and P lies at second last index of string. string.size() - 2.
Remove AM/PM from the given string.
If the time is given in AM (Then only 12 AM will need to be addressed)
If time is given in PM - Then there are two cases:
i. When time is from 01 PM - 11 PM (Simply add 12 in the hrs)
ii. When time is 12 PM (Skip this time as it is already in 24 hrs format)
string timeConversion(string s) {
int n = s.size();
bool isAM = (s.substr(n - 2, 1) == "A") ? true : false;
s = s.substr(0, n-2);
Time Conversion
You are viewing a single comment's thread. Return to all comments →
//Language = C++ //Approach
Extract the A or P from the given time string. A and P lies at second last index of string. string.size() - 2. Remove AM/PM from the given string.
If the time is given in AM (Then only 12 AM will need to be addressed)
If time is given in PM - Then there are two cases: i. When time is from 01 PM - 11 PM (Simply add 12 in the hrs) ii. When time is 12 PM (Skip this time as it is already in 24 hrs format)
string timeConversion(string s) { int n = s.size(); bool isAM = (s.substr(n - 2, 1) == "A") ? true : false; s = s.substr(0, n-2);
}