-1
 #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;
            ## 

This code converts a 12 hour clock format to 24 hour clock format

##}

nishchalpro
  • 93
  • 12

1 Answers1

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);
Community
  • 1
  • 1
Mathieu
  • 8,840
  • 7
  • 32
  • 45