Time Conversion

  • + 5 comments

    Your logic is inelegant; you are needlessly checking for both AM and PM. Using an else-if statement after checking PM ensures that the AM logic will only be reached/tested if the PM check failed, thus eliminating needless/redundant condition checking. Here is modified logic:

    if ( strcmp(t12,"PM") == 0 ){
        hh += (hh != 12) ? 12 : 0;
    }
    else if(hh==12){ hh = 0; }
    

    I would also recommend you follow the best practice of always bracketing the 'then' code in your conditional statements (if(){}), regardless of how little code it may contain.