Array ( [0] => [1] => questions [2] => Strings [3] => Timing-Format )
Given a String. You have to convert the given time i,e, 12hours format to 24 hours format.
First line contains a string.
Print the resultant time in 24 hours format.
2.30 PM
14.30
11.00 AM
11.00
8.00 PM
20.00
Login to see Discussion
Step-i) get the length of the given string
Step-ii) create a empty string new_time
Step-iii) if last second character of the string is P go to next step else go to vii
Step-iv) get the hour part from the string
Step-v) if hours equal to 12 append 00 to new_time and copy the rest of the string
Step-vi) else copy the entire string and return the time
Step-vii) get the hours part from the string
Step-viii) add 12 to the hours part and append to the new_time
Step-ix) add rest of the string as it is to the new_time
Step-x)return the string
Time Complexity : O(1)
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution