Strstr_while model, strstr

Source: Internet
Author: User

Strstr_while model, strstr

1 # define _ CRT_SECURE_NO_WARNINGS 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 6 int method () 7 {8 char * p = "123abcd23sdfwee1_abcd12344abcd"; 9 int ncount = 0; 10 11/* do12 {13 p = strstr (p, "abcd"); 14 if (p! = NULL) 15 {16 ncount ++; 17 p = p + strlen ("abcd"); 18} 19 else20 {21 break; 22} 23} while (* p! = '\ 0'); */24 25 26 while (p = strstr (p, "abcd") 27 {28 ncount ++; 29 p = p + strlen ("abcd"); 30 if (* p = '\ 0') 31 break; 32} 33 34 printf ("ncount: % d \ n ", ncount); 35 printf (" hello... \ n "); 36 system (" pause "); 37 return 0; 38} 39 int getCount (char * mystr/* in */, char * sub/* in */, int * ncount) 40 {41 int ret = 0; 42 int tmpCount = 0; 43 // initialize p pointer to the search condition 44 char * p = mystr; // do not change the value of the parameter easily 45 if (mystr = NULL | sub = NULL | | Ncount = NULL) 46 {47 ret =-1; 48 printf ("func getCount () err: % d (mystr = NULL | sub | NULL | ncount = NULL) \ n ", ret); 49 return ret; 50} 51 52 while (p = strstr (p, sub) 53 {54 tmpCount ++; 55 p = p + strlen (sub ); 56 if (* p = '\ 0') 57 break; 58} 59 * ncount = tmpCount; // The indirect value assignment indicates the existence of the pointer 60 return ret; 61} 62 63 int main () 64 {65 char * p = "abcd1234asd222abcd"; 66/* char * sub; 67 sub = "abcd "; */68 char sub [] =" Abcd "; 69 int ncount = 0; 70 int ret = 0; 71 72 73 ret = getCount (p, sub, & ncount); 74 if (ret! = 0) 75 {76 printf ("func getCount () err: \ n", ret); 77 78} 79 printf ("ncount: % d \ n", ncount ); 80 81 printf ("hello... \ n "); 82 system (" pause "); 83 return 0; 84}

 

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.