#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
int hh,mm,ss;
char ap[2];
scanf("%d%d%d%s",&hh,&mm,&ss,ap);
if(stricmp(ap,"AM")!=0)
{
hh+=12;
}
printf("%d:%d:%d",hh,mm,ss);
return 0;
##
##}
Insufficient string space. Also add limit to scanning
char ap[3];
scanf("%d%d%d%2s",&hh,&mm,&ss,ap);
stricmp
is not standard. Use strcasecmp
on linux or _stricmp
on windows.
Functionally missing the handling of times like 12 34 56 AM which should change to 0 34 56.
if(stricmp(ap,"AM")==0) {
if (hh >= 12) hh -= 12;
} else if(stricmp(ap,"PM")==0) {
hh +-= 12;
}
More usual to print minutes.seconds with leading zeros.
printf("%d:%02d:%02d",hh,mm,ss);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.