Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <limits.h>
- #include <math.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char* readline();
- /*
- * Complete the timeConversion function below.
- */
- /*
- * Please either make the string static or allocate on the heap. For example,
- * static char str[] = "hello world";
- * return str;
- *
- * OR
- *
- * char* str = "hello world";
- * return str;
- *
- */
- char* timeConversion(char* s) {
- int lastchar=strlen(s)-1;//position of last char in the array
- char* pplast=s+(lastchar-1);//pointer to previous of last char so can check (A||P)
- char* fDigit=s;char* sDigit=fDigit+1;//first and second digit
- int ifDigit=*fDigit-'0';
- int isDigit=*sDigit-'0';
- int concat=(ifDigit*10+isDigit);
- if ((*pplast=='P'&&concat!=12)||(*pplast=='A'&&concat==12)){
- int plus=(concat==12)?-12:12;
- int sum;
- sum=concat+plus;
- int f,s;
- f=(sum/10);
- s=(sum%10);
- *fDigit=f+'0';
- *sDigit=s+'0';
- }
- *pplast='\0';
- return s;
- }
- int main()
- {
- FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
- char* s = readline();
- char* result = timeConversion(s);
- fprintf(fptr, "%s\n", result);
- fclose(fptr);
- return 0;
- }
- char* readline() {
- size_t alloc_length = 1024;
- size_t data_length = 0;
- char* data = malloc(alloc_length);
- while (true) {
- char* cursor = data + data_length;
- char* line = fgets(cursor, alloc_length - data_length, stdin);
- if (!line) { break; }
- data_length += strlen(cursor);
- if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
- size_t new_length = alloc_length << 1;
- data = realloc(data, new_length);
- if (!data) { break; }
- alloc_length = new_length;
- }
- if (data[data_length - 1] == '\n') {
- data[data_length - 1] = '\0';
- }
- data = realloc(data, data_length);
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement