0

Can Anyone Explain this code to me? I have understood the if and else if part but I'm stuck on the else part inside the else if. I can't understand what are we subtracting here. The time is of the format 08:33:45AM(/PM).

char* timeConversion(char* s) {
    /*
     * Write your code here.
     */
    if (s[8]=='A')
    {
        if (s[0] =='1'&&s[1] =='2')
        {
            s[0] = '0';
            s[1] = '0';
        }
        s[8] = '\0';
    }
    else if (s[8] == 'P')
    {
        if (s[0]=='1' && s[1] =='2')
        {
            s[8] = '\0';
            return s;
        }

what are we subtracting here?

        s[0] = s[0]-'0'+'1';
        s[1] = s[1] - '0' +'2';
        s[8] = '\0';
    }
    else 
    {
        return "Error";
    }
    return s;
}
susanth29
  • 356
  • 1
  • 2
  • 17

1 Answers1

2

I am assuming the question is to convert time from HH:MM:SSAM(PM)(12 hour) to HH:MM:SS (24 hour). If so, your code works well for most of the input but fails in some cases. First, let's explain how the code works.

char* timeConversion(char* s) {
    // we check s[8] because in HH:MM:SSAM(PM) we can check whether
    // it is AM or PM by checking s[8] which is 'A' or 'P' respectively
    if (s[8]=='A')
    {
        // if it is 'AM' we don't need to change much except for these cases
        // 12:01:23AM is converted to 00:01:23
        // and that is what we are doing in the if part.
        if (s[0] =='1'&&s[1] =='2') 
        {
            s[0] = '0';
            s[1] = '0';
        }
        s[8] = '\0'; 
        // we are terminating the string at s[8], leaving only HH:MM:SS
    }
    else if (s[8] == 'P')
    {
        // 12:01:13PM is converted to 12:01:13. so we don't need to change 
        // anything if s[0] is '1' and s[1] is '2'.
        if (s[0]=='1' && s[1] =='2')
        {
            s[8] = '\0';
            return s;
        }
        // 04:24:56PM is changed to 16:24:56. so we need to add 1 to s[0]
        // and 2 to s[1] and that is done here.
        s[0] = s[0]-'0'+'1';
        s[1] = s[1] - '0' +'2';
        s[8] = '\0';
        // However this fails if the input is 08:23:55PM as the output will be 
        // 1::23:55 (ASCII value of '8' is 56 and ASCII value of ':' is 58)
        // and that is wrong answer. The correct answer is 20:23:55.
        // instead try changing first two characters to number and add 12 to it
        // and change the resultant number back to charecters.
    }
    else 
    {
        // if s[8] is neither 'A' nor 'P', then the input is wrong.
        return "Error";
    }
    return s;
}

And you can change the code in the else if part to

int n = (s[0]-'0')*10 + (s[1]-'0');
n += 12;
s[0] = '0' + (n/10);
s[1] = '0' + (n%10);
s[8] = '\0';
susanth29
  • 356
  • 1
  • 2
  • 17
  • 1
    This is a good explanation. Because of assumptions, I suspect it's also very dangerous to call this. I would expect that a parser reading the whole string would be much safer. – Neil Aug 10 '21 at 04:25