How to extract a substring from a string in C?

后端 未结 5 1616
野的像风
野的像风 2020-12-11 04:15

I tried using strncmp but it only works if I give it a specific number of bytes I want to extract.

char line[256] = This \"is\" an example. //I want to extr         


        
5条回答
  •  青春惊慌失措
    2020-12-11 04:37

    #include 
    ...        
    substring[0] = '\0';
    const char *start = strchr(line, '"') + 1;
    strncat(substring, start, strcspn(start, "\""));
    

    Bounds and error checking omitted. Avoid strtok because it has side effects.

提交回复
热议问题