fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char* extractValueInLine(char* line) {
  6. int len = strlen( line );
  7. char* res = malloc(1);
  8. res[0] = '\0';
  9. unsigned quoteCpt = 0;
  10. for (unsigned i = 0; i < len; i++){
  11. if (line[i] == '\"')
  12. quoteCpt++;
  13. else if (quoteCpt == 3) // If 3 quotes has been skipped
  14. {
  15. res = realloc(res, strlen(res) + sizeof(char) + 1); // Free memory for additional char
  16. if (NULL == res)
  17. fprintf(stderr, "Not enough memory.\n");
  18. else
  19. {
  20. char toAppend[2];
  21. toAppend[1] = '\0';
  22. toAppend[0] = line[i];
  23. strcat(res, toAppend);
  24. }
  25. }
  26. }
  27. return res;
  28. }
  29.  
  30.  
  31. int main(void) {
  32. // your code goes here
  33. printf("%s", extractValueInLine("\"key\":\"value\""));
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
value