|
发表于 2016-12-12 13:40:43
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- int search(char str[], char sub_str[])
- {
- int ret = 0;
- int s = 0;
- int str_len = strlen(str);
- int sub_str_len = strlen(sub_str);
- for(int i = 0; i < str_len; i++)
- {
- for(int j = 0; j < sub_str_len; j++)
- {
- if(str[i + j] == sub_str[j])
- {
- s++;
- }
- else
- {
- s = 0;
- break;
- }
- }
- if(s == sub_str_len)
- {
- ret++;
- }
- }
- return ret;
- }
- int main(void)
- {
- char *str = "asd asasdfg,asd as 16*67 asd10";
- char *sub_str = "as";
- int c = search(str, sub_str);
- printf("子字符串 %s 共出现了%d次\n", sub_str, c);
- return 0;
- }
复制代码 |
|