Time Conversion

  • + 3 comments

    Same number of lines, less variables, no string comparisons. One atoi though.

    int main() {

    char time[11] = {0};
    scanf("%s", time);
    int n_hour = atoi(time);
    char plus12 = n_hour >= 12;
    n_hour += time[8] == 'P' ? (plus12 ? 0 : 12) : (plus12 ? -12 : 0);
    printf("%02d:%.*s:%.*s", n_hour, 2, time + 3, 2, time + 6);
    return 0;
    

    }