"C Language" extracts substrings

Source: Internet
Author: User

 write a function that extracts a substring from a string. The function prototype is as follows: Int substr (Char dst[], char src[],int start, int len) {} target: from   src  the starting position of the array starts at the position of the  start character backward, copying up to  len  non-nul characters to the &NBSP;DST array. After copying is complete, 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 =  ' + '; return ret;} Int main () {char *p =  "Bit-tech"; char arr[10];char array[10] = {0};int  ret = substr (arr, p, 4,5);p rintf ("%d\n",  ret);p rintf ("%s\n",  arr); System (" Pause "); return 0;} 


This article is from the "vs LV" blog, so be sure to keep this source http://survive.blog.51cto.com/10728490/1706007

"C Language" extracts substrings

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.