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;
}