[C Language] extract substring
Compile a function that extracts a substring from a string. The function is prototype: int substr (char dst [], char src [], int start, int len) {}. The target is: start from the start position of the src array and offset it back to the start character. A maximum of len non-NUL characters can be copied to the dst array. After copying, the dst array must end with NUL bytes. The Return Value of the function is the length of the string stored in the dst array. Code implementation: # include <stdio. h> # include <assert. h> int substr (char dst [], char src [], int start, int len) {assert (dst); assert (src); int ret = 0; while (start) {src ++; start --;} if (strlen (src) <len) {len = strlen (src);} ret = len; while (len) {* dst ++ = * src ++; len --;} * dst = '\ 0'; return ret;} int main () {char * p = "bit-tech"; char arr [10]; char array [10] = {0}; int ret = substr (arr, p, 4, 5 ); printf ("% d \ n", ret); printf ("% s \ n", arr); system ("pause"); return 0 ;}