[Statement: This article is only intended for self-Summary and mutual communication, and may be omitted. Email: Mr_chenping@163.com]
Question:
A known string, such as asderwsde, is used to search for a substring, such as the number of sde. If no value is returned, the number of sub-strings is returned.
Question Analysis:
1. traverse the string sequentially and then compare it with strncmp.
Algorithm Implementation:
# Include
# Include
Int sub_str_count (const char * str, const char * sub_str) {int str_len = strlen (str); int sub_str_len = strlen (sub_str); int count = 0; int times = str_len-sub_str_len + 1; while (times --) {if (! Strncmp (str, sub_str, sub_str_len) count ++; str ++;} return count ;} int main (int argc, char * argv []) {printf ("% s ---- % s ---> % d \ n", argv [1], argv [2], sub_str_count (argv [1], argv [2]); return 0 ;}