C language Simulation implements STRSTR function, Strrstr function

Source: Internet
Author: User

Strstr and Strrstr have been relatively difficult in the string, but as long as we are good at analysis, the anatomy of the string, it will be very easy. In fact, the process of learning code needs us to calm down to analyze, understand.

function and usage of SRTSTR function

Prototype:char *strstr (const char *DST, const char *src);

Returns the pointer to the location

 #include  <stdio.h> #include  <string.h> #include   <assert.h>char *my_strstr (CONST&NBSP;CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) {const  char *str1 = dst;const char *str2 = src;const char *fast =  null;assert (DST), assert (SRC);while  (*str1) {fast = str1;while  (*str1 && &NBSP;*STR2&NBSP;&&&NBSP;*STR1&NBSP;==&NBSP;*STR2) {str1++;str2++;} if  (*str2 ==  ') return  (char*) fast;str1 = fast+1;str2 = src;}      return null;} Int main () {char arr1[] =  "ABCDEFGDEFK";char arr2[] =  "DEFK"; char *ret  = my_strstr (ARR1,ARR2);if  (*ret) {puts (ret);} return 0;} 

Parsing code:

Define two string arrays in the main function, char arr1[]= "ABCDEFGDEFK", Char arr2[]= "DEFK", receive with pointers in the calling function, the advantage of pointer reception is that the pointer points to the first address of the string, the pointer is best protected with a const. In case of being destroyed. Define three pointers in the calling function, char *sr1=dst,char* str2=src,char* fast=null (avoid wild pointers), use to assert if the pointer exists before using, if the contents of str1 exist into the loop, first fsat=str1; If str1 points to content equal to str2 point, str1,str2 address Gaga, if the content of str2 is equal to "", the contents of the STR2 string appears in the STR1 string, otherwise str1 points to the last address of the fast pointer to the address, STR2 returns to its first address and resumes the loop until the STR2 string appears for the first time in str1.

Simulating the implementation of the STRRSTR function

Prototype:char *strrstr (const char *STR1, const char *STR2);

Find the last position of the str2 string in the STR1 string (not including the string terminator of the str2). The pointer If not found, returns a null pointer.

 #include  <stdio.h> #include  <string.h> #include   <assert.h>char *my_strrstr (CONST&NBSP;CHAR&NBSP;*DST,CONST&NBSP;CHAR&NBSP;*SRC) {CHAR&NBSP;*STR1=DST ; Char *str2=src;char *fast=null;char *last=null;assert (DST); assert (SRC);while  (*str1) {fast =str1;while  (*STR1&&*STR2&&*STR1==*STR2) {str1++;str2++;} if  (*str2 ==  ') last=fast;str1=fast+1;str2 = src;} if  (*str1 ==  ') return /* (char *) */last;/*return null;*/}int main () {Char  *qwe= "ASDFGHASDGFDFGDFGDFGDFGDFG"; char *zaq= "DFG"; Char *ret=my_strrstr (Qwe,Zaq);p UTS (ret); return 0;} 



This article is from "Dream" blog, please be sure to keep this source http://12951882.blog.51cto.com/12941882/1976515

C language Simulation implements STRSTR function, Strrstr function

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.