[C Language] extract substring

Source: Internet
Author: User

[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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.