Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- char time[10];
- int i, frst_dgit, scnd_dgit, temp;
- scanf("%s", time);
- if(time[8] == 'P' && time[9] == 'M')
- {
- if(time[0] == '1' && time[1] == '2')
- {
- for(i = 0; i < 8; i++) printf("%c", time[i]);
- ///in 24 hour format we don't need last two character PM or AM, so conduct the loop until 7th index
- }
- else
- {
- scnd_dgit = time[1] - '0';
- //convert the character into integer, first two character indicates hour and that the things we have to convert into 24 hour format
- frst_dgit = time[0] - '0';
- //we have to add 12 with given hour, so we will add 1 with first digit of hour and 2 with second digit of hour
- if(scnd_dgit + 2 >= 10)
- {
- //if any value of second digit stands greater than 10 after adding two , we have to subtract 10 and add one with first digit
- time[1] = (scnd_dgit + 2 - 10)+ '0';
- ///as greater than 10 , we subtract it from second digit and add one with first digit
- time[0] =(frst_dgit + 1 + 1)+ '0';
- for(i = 0; i < 8; i++) printf("%c", time[i]);
- }
- else
- {
- time[1] = (scnd_dgit + 2) + '0';
- time[0] = (frst_dgit + 1) + '0';
- for(i = 0; i < 8; i++) printf("%c", time[i]);
- }
- }
- }
- else if(time[8] == 'A' && time[9] == 'M')
- {
- if(time[0] == '1' && time[1] == '2')
- {
- time[0]=time[1]='0';
- //according to the condition in the question if 12:00:00AM then in 24 hour format it will be 00:00:00, so we made first two character zero
- for(i = 0; i < 8; i++) printf("%c", time[i]);
- }
- else
- for(i = 0; i < 8; i++) printf("%c", time[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement