Does C have anything like the `substr extract substrin routine present in other languages?

Q

Does C have anything like the `substr extract substrin routine present in other languages?

✍: Guest

A

Not as such. To extract a substring of length LEN starting at index POS in a source string, use something like
char dest[LEN+1];
strncpy(dest, &source[POS], LEN);
dest[LEN] = '\0'; /* ensure \0 termination */
char dest[LEN+1] = "";
strncat(dest, &source[POS], LEN);

or, making use of pointer instead of array notation,
strncat(dest, source + POS, LEN);
(The expression source + POS is, by definition, identical to &source[POS]

2016-03-07, 1110👍, 0💬