The number of times a string substring appears in C language.

Source: Internet
Author: User

The number of times a string substring appears in C language.

# Include <stdio. h>
# Include <string. h>
Int substring (char * str, char * str1); // function prototype
Int main (void)
{
Char str [64] = {0 };
Char str1 [16] = {0 };
Int I, j, x;
Printf ("please put the string \ n ");
Gets (str); // enter the original string
Puts (str );
Printf ("\ n ");
Printf ("please put the string1 \ n ");
Gets (str1); // substring in the input string
Puts (str1 );
Printf ("\ n ");
I = strlen (str); // original String Length
J = strlen (str1); // substring Length
Printf ("the string lenth is % d \ n", I );
Printf ("the string lenth is % d \ n", j );
X = substring (str, str1 );
Printf ("then anwser is % d \ n", x );
Return 0;
}
Int substring (char * str, char * str1)
{
Int x = 0;
Char * p; // any attached Initial Value
Do {
P = strstr (str, str1); // The 1. p Pointer Points to the return value of strstr. 3. The function parameters change once again, and p points to the strstr return value again.
If (p! = NULL ){
Str = p + 1; // 2. str also points to the next address of the return value p of strstr.
X = x + 1;
}
}
While (p! = NULL );
Return x;
}

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.