Time Conversion

  • + 1 comment

    Stream approach:

    int main(){
        string time;
        int hours,seconds,minutes;
        string merid;
        char delim;
        stringstream ss;
    
        cin >> time;
        ss << time;
        ss >> hours >> delim >> minutes >> delim >> seconds >> merid;
    
        if((merid=="PM"&&hours!=12)||(merid=="AM"&&hours==12))
            hours=(hours+12)%24;
    
        printf("%02d:%02d:%02d", hours, minutes, seconds);
    
        return 0;
    }
    

    Critique welcome!