You are viewing a single comment's thread. Return to all comments →
void ConvertToMilitarTime(string am_pm, string& hours) { uint8_t digit_sum = hours[0] + hours[1]; if(am_pm == "AM") { if(digit_sum < TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } else if(digit_sum == TWELVE_ASCII) { hours[0] = ZERO_ASCII; hours[1] = ZERO_ASCII; } } else // is PM { if(digit_sum != TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } } } /* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */
string timeConversion(string s) { string am_pm(s, 8); string min_sec(s, 2, 6); string hours_digits(s, 0, 2);
ConvertToMilitarTime(am_pm, hours_digits); string formated_time = hours_digits + min_sec; return formated_time;
}
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 →
define TWELVE_ASCII 99 // "1" + "2" = 49+50 = 99
define ZERO_ASCII 48
void ConvertToMilitarTime(string am_pm, string& hours) { uint8_t digit_sum = hours[0] + hours[1]; if(am_pm == "AM") { if(digit_sum < TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } else if(digit_sum == TWELVE_ASCII) { hours[0] = ZERO_ASCII; hours[1] = ZERO_ASCII; } } else // is PM { if(digit_sum != TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } } } /* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */
string timeConversion(string s) { string am_pm(s, 8); string min_sec(s, 2, 6); string hours_digits(s, 0, 2);
}