C Language Simulation library function strstr () function

Source: Internet
Author: User

The Simulation   library function   strstr () function//from the parent string (longer) found   exactly the same substring (shorter);//Returns the address of the first character of the same string in the parent string; #include <stdio.h >char * my_strstr (char arr[],char arr1[]) {char *p1=null,*p2=null,*p=null;int  I = 0,j = 0,k=0,m=0;p1 =&arr[0];p 2 = &arr1[0];while  (1) {if   (arr1[0] ==  ') {printf ("The valid length of the sub-array is incorrect! \ n "); return null;} if (*p2== ') break;        i++;p 2++;} while  (1) {if  (arr[0] ==  ') {printf ("The valid length of the parent array is incorrect! \ n "); return null;} if (*p1== ') break;j++;p 1++;} if  (i > j) {printf ("The word group is larger than the parent array! Unable to query! \ n "); return null;} P1 =&arr[0];p 2 = &arr1[0];j=0;while  (1) {if  (*P1&NBSP;==&NBSP;*P2) {p =  p1;p1++;p 2++;m=0;k++;} Else{p1++;j++;if (m==0) {m=1;k=0;p2=&arr1[0];}} if  (k == i)     return p-k+1;if  (p1 == null | |  p2 == null| | *p1== ' + ')break;}     return null;} Int main () {char arr[20] =  "0";char arr1[20] =  "0";char* p =  null;printf ("Please enter a string within 20 characters:"), gets (arr);p rintf ("Please enter a substring of the query:"), gets (arr1);p  = my_strstr (arr,  ARR1);if  (p == null) {printf ("Your input has a problem or failed to find!") \ n ");} Else    printf ("The first element of the substring in the parent string address is:%p\n", p);p rintf ("arr array first element address is (self-counting):%p\n", &arr[0]); return 0;}


C Language Simulation library function strstr () function

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.