Given a time in AM/PM format, convert it to military (24 -hour) time.
Note: Midnight is12:00:00 AM on a 12 -hour clock and 00:00:00 on a 24 -hour clock. Noon is 12:00:00 PM on a 12 -hour clock and 12:00:00 on a 24 -hour clock.
Note: Midnight is
Input Format
A time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01≤hh≤12 .
Sample Input
Convert and print the given time in 24 -hour format, where 00≤hh≤23 .
Sample Output
07:05:45PM
Explanation
19:05:45
--------------------------------------------------------------------------
Time Conversion - Hacker Rank Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int hh, mm, ss ;
char t12[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;
if (strcmp(t12,"PM")==0 && hh!=12) hh += 12 ;
if (strcmp(t12,"AM")==0 && hh==12) hh = 0 ;
printf("%02d:%02d:%02d", hh, mm, ss) ;
return 0;
}
Is the code below right?:
ReplyDelete#include
int main()
{
char s[10240];
gets(s);
s[0]=1+s[0];
s[1]=2+s[1];
s[8]='\0';
puts(s);
return 0;
}
sorry..forgot to add ..
DeleteI tried this code in hacker rank "time conversion" in algorithm section.It gave wrong answer for few test ....i dont know why!!!!please somebody tell wheres the problem
it is valid for test case
Delete07:45:05PM
09:45:05PM -> 21:45:05, so in some case you should add 2 to s[0].
Deletes[0]=1+s[0]
should be
s[0]=1+s[0] + (2+s[1])/10;
Wrong ans..
ReplyDeleteCHANGE t12[2] to t12[20].
Deletemeans chane the size of array
thankyou ,it work but wha's the logic behind this.
Deletethis passes all testcases.
ReplyDeletestatic String timeConversion(String s) {
String[] timesplits = s.split(":");
int hours = Integer.parseInt(timesplits[0]);
String meridian = timesplits[timesplits.length -1];
String hoursString = "";
if(meridian.contains("AM")) {
hoursString = hours == 12 ? "00": timesplits[0];
} else if(meridian.contains("PM")) {
hours = hours + 12;
hoursString = hours == 24 ? "12": hours;
}
return hoursString+":"+timesplits[1]+":"+timesplits[2].substring(0,2);
}
static String timeConversion(String s) {
ReplyDeleteString[] timesplits = s.split(":");
int hours = Integer.parseInt(timesplits[0]);
String meridian = timesplits[timesplits.length -1];
String hoursString = "";
if(meridian.contains("AM")) {
hoursString = hours == 12 ? "00": timesplits[0];
} else if(meridian.contains("PM")) {
hours = hours + 12;
hoursString = hours == 24 ? "12": hours+"";
}
return hoursString+":"+timesplits[1]+":"+timesplits[2].substring(0,2);
}
The most important distinction between military and regular time would be the way hours are all expressed. Standard time uses statistics inch to 1 2 to identify every one of those twenty four hours in a day. In armed forces time, the hours have been numbered from 00 to 23. Below this system, midnight is 00, 1 a.m. is 01, inch p.m. is 13, and so on.
ReplyDeleteRoutine and army time say minutes and moments in exactly the same way. When converting out of regular to army timing and vice versa, the minutes and moments don't change.
Standard timing demands the use of a.m. and p.m. to definitely recognize the period . Given that military time utilizes a exceptional two-digit range to identify each of the 24 hours in a day, a.m. and p.m. are not unnecessary.
The following table summarizes the association between regular and military time.
why strcmp()==0? what does that == 0 means actually?
ReplyDeleteIt is a property of string compare attribute means if two strings are same there is 0 difference
DeleteAbove solution mentioned in article by author is wrong, there should be t12[3] instead of t12[2].
ReplyDeletewhy? can u explain
Deletedef staircase(s):
ReplyDeletes1 = s.split(':')
if s[-2:] == 'PM':
s1[0] = int(s1[0]) + 12
s1[0] = str(s1[0])
s1[2] = s1[2][:2]
elif s[-2:] == 'AM':
s1[2] = s1[2][:2]
else:
pass
print(':'.join(s1))
The time is taken as string then can u pls tell me how to add 12 to that 7
ReplyDeletetime coversion hackerrank solution in c,in asimple way :
ReplyDelete#include
int main()
{int i,l;
char a[89];
gets(a);
for(i=0;a[i];i++)
{
if(a[i]=='P' && a[i+1]=='M' )
{
if(a[0]=='1' && a[1]== '2')
{
}
else
{
a[0]+=1;
a[1]+=2;
}
}
if(a[i]=='A' && a[i+1]=='M' )
{
if(a[0]=='1' && a[1]== '2')
{
a[0]='0';
a[1]='0';
}
}
}
a[8]='\0';
puts(a);
}
ReplyDelete// this is correct sollution
#include
#include
#include
#include
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char timestamp[11] = "\0\0\0\0\0\0\0\0\0\0\0";
int hr=0;
scanf("%s", timestamp);
if(timestamp[8] == 'P'){
hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
if(hr < 12) hr += 12;
}
else{
hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
if(hr == 12) hr = 0;
}
timestamp[0] = hr/10 + '0';
timestamp[1] = hr%10 + '0';
timestamp[8] = '\0';
timestamp[9] = '\0';
printf("%s", timestamp);
return 0;
}
CORRECTION
ReplyDeletechar t12[20];
#!/bin/python3
ReplyDeleteimport os
import sys
import re
#
# Complete the timeConversion function below.
def timeConversion(s):
if s[len(s)-2:]=='PM':
if s[0] == '0':
s1=s.strip('0')
sn = s1.strip('PM')
return sn.replace(sn[0],str(int(sn[0])+12))
elif s[0:2]=='12':
return s.strip('PM')
else:
sn = s.strip('PM')
return sn.replace(sn[0:2],str(int(sn[0:2])+12))
else: #s[len(s)-2:]=='AM':
if s[0:2]=='12':
s1=s.strip('AM')
return s1.replace(s[0:2],'00')
else:
return s.strip('AM')
if __name__ == '__main__':
f = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = timeConversion(s)
f.write(result + '\n')
f.close()
#include
ReplyDelete#include
#include
#include
#include
#include
#include
int main() {
int hh, mm, ss ;
char t12[3];
scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;
if (strcmp(t12,"PM")==0 && hh!=12) hh += 12 ;
if (strcmp(t12,"AM")==0 && hh==12) hh = 0 ;
printf("%02d:%02d:%02d", hh, mm, ss) ;
return 0;
}
Go through that guys it works!!