Created
September 4, 2020 22:57
-
-
Save MichaelSDavid/764a09e0919b6090227976e7f2d0afac to your computer and use it in GitHub Desktop.
SoloLearn Code Coach solution for Military Time in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
int main() { | |
string twelve_hr_str; | |
string initial_hr; | |
int new_hr; | |
string final_24_str; | |
getline(cin, twelve_hr_str); | |
if (twelve_hr_str.find("PM") != string::npos) { | |
if (twelve_hr_str.length() == 8) { | |
initial_hr = twelve_hr_str.substr(0,2); | |
new_hr = stoi(initial_hr) + 12; | |
final_24_str = to_string(new_hr) + twelve_hr_str.substr(2,4); | |
} else { | |
initial_hr = twelve_hr_str.substr(0,1); | |
new_hr = stoi(initial_hr) + 12; | |
final_24_str = to_string(new_hr) + twelve_hr_str.substr(1,4); | |
} | |
} else { | |
if (twelve_hr_str.length() == 8) { | |
final_24_str = twelve_hr_str.substr(0,5); | |
} else { | |
final_24_str = "0" + twelve_hr_str.substr(0,5); | |
} | |
} | |
cout << final_24_str << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment