Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepted Solution to the problem: Time-Conversion #2514

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Hackerank/C++/Time-Conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <bits/stdc++.h>

using namespace std;

/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/

string timeConversion(string s)
{
char str[] = {"00:00:00\0"};
if (s[8] == 'A')
{
if (s[0] == '1' && s[1] == '2')
{
for (int i = 2; i < 8; i++)
{
str[i] = s[i];
}
str[8] = '\0';
return str;
}
else
{
for (int i = 0; i < 8; i++)
str[i] = s[i];
str[8] = '\0';
return str;
}
}
else
{
if (s[0] == '1' && s[1] == '2')
{
for (int i = 0; i < 8; i++)
str[i] = s[i];
str[8] = '\0';
return str;
}
else
{
string str1;
for (int i = 0; i < 2; i++)
str1[i] = s[i];
int myint1 = stoi(str1);
myint1 += 12;
string temp = to_string(myint1);
for (int i = 0; i < 2; i++)
str[i] = temp[i];
for (int i = 2; i < 8; i++)
str[i] = s[i];
str[8] = '\0';
return str;
}
}
return str;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string s;
getline(cin, s);

string result = timeConversion(s);

fout << result << "\n";

fout.close();

return 0;
}