Problem Statement
You are given time in AM/PM format. Convert this into a 24 hour format.
Note Midnight is 12:00:00AM or 00:00:00 and 12 Noon is 12:00:00PM.
Input Format
Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM
where
01≤hh≤12
00≤mm≤59
00≤ss≤59
where
Output Format
You need to print the time in 24 hour format i.e. hh:mm:ss
where
00≤hh≤23
00≤mm≤59
00≤ss≤59
where
Sample Input
07:05:45PM
Sample Output
19:05:45
Solution:-
#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 str[100];
const char s[2]=":";
char ans[10],ans1[10],ans3[10];
char ss[10];
char read;
char *h;
int arr[2],i=0;
scanf("%s",str);
// printf("%s",str);
h=strtok(str,&s);
//printf("%s",h);
while(h!=NULL)
{
if(i==2)
{
strcpy(ss,h);
//printf("%s",ss);
read=ss[2];
ss[2]='\0';
strcpy(ans3,ss);
arr[i]=atoi(ss);
// printf("%d\n",arr[i]);
}
else{
// printf("%d\n",arr[i]);
if(i==0)
{
strcpy(ans,h);
}
if(i==1)
{
strcpy(ans1,h);
//printf("%s",ans);
}
arr[i]=atoi(h);
// ans++;
}
i++;
h=strtok(NULL,&s);
}
if(read=='A'&&arr[0]==12)
{
arr[0]=00;
strcpy(ans,"00");
}
if(read=='P')
{
if(arr[0]!=12)
arr[0]=arr[0]+12;
}
if((1<=arr[0]<=12)&&(0<=arr[1]<=59)&&(0<=arr[2]<=59))
{
if(read=='P')
{
if(arr[0]==12)
printf("%s:",ans);
else
printf("%d:",arr[0]);
printf("%s:",ans1);
printf("%s\n",ans3);
}
else
{
printf("%s:",ans);
printf("%s:",ans1);
printf("%s",ans3);
}
}
// int hh=atoi()
return 0;
}
No comments:
Post a Comment