Time Conversion HackerRank Solution in C, C++, Java, Python

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.

– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

Example

  • s = ‘12:01:00PM’

Return ’12:01:00′.

  • s = ‘12:01:00PM’

Return ’00:01:00′.

Function Description

Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

  • string s: a time in 12 hour format

Returns

  • string: the time in 24 hour format

Input Format

A single string s that represents a time in 12-hour clock format (i.e.:hh:mm:ssAM  or hh:mm:ssPM).

Constraints

  • All input times are valid

Sample Input 0

07:05:45PM

Sample Output 0

19:05:45

Time Conversion HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

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;
}

Time Conversion HackerRank Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    string s;
    string h;
    int hr;
    cin>>s;
    hr = ((s[0]-'0')*10)+(s[1]-'0');
    if(s[8]=='P'&&s[9]=='M'&& hr ==12) cout<<to_string(hr);
    else if(s[8]=='P'&&s[9]=='M') cout<<to_string(hr+12);
    else if(s[8]=='A'&&s[9]=='M'&&hr==12) cout<<"00";
    
    else cout<< s[0]<<s[1];
    
   
    for(int i =2;i<8;i++)
        cout<<s[i];
    cout<<endl;
    return 0;
}

Time Conversion HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

   	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine(); //07:05:45PM
        DateFormat inFormat = new SimpleDateFormat( "hh:mm:ssaa");
        DateFormat outFormat = new SimpleDateFormat( "HH:mm:ss");

        Date date = null;
        try {
            date = inFormat.parse(s); 
        }catch (ParseException e ){
            e.printStackTrace();
        }
        if( date != null ){
            String myDate = outFormat.format(date);
            System.out.println(myDate);
        }
    }

}

Time Conversion HackerRank Solution in Python

import re

time = raw_input()
times = re.search(r"(?P<hh>..):(?P<mm>..):(?P<ss>..)(?P<ampm>..)", time)
hour = times.group('hh')

if times.group('ampm') == 'PM':
    if hour == '12':
        hr = '12'
    else:
        hr = str(int(hour) + 12)
else:
    if hour == '12':
        hr = '00'
    else:
        hr = hour

print hr + ':' + times.group('mm') + ':' + times.group('ss')

Time Conversion HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        Console.WriteLine(DateTime.Parse(Console.ReadLine()).ToString("HH:mm:ss"));
    }
}

 

Attempt – Time Conversion HackerRank Challenge

Link: https://www.hackerrank.com/challenges/time-conversion

Leave a Comment