strstr_while模型

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int method()
{
char *p = "123abcd23sdfwee131abcd12344abcd";
int ncount = ; /*do
{
p = strstr(p, "abcd");
if (p != NULL)
{
ncount++;
p = p + strlen("abcd");
}
else
{
break;
}
} while (*p!='\0');*/ while (p = strstr(p,"abcd"))
{
ncount++;
p = p + strlen("abcd");
if (*p=='\0')
break;
} printf("ncount:%d\n", ncount);
printf("hello...\n");
system("pause");
return ;
}
int getCount(char *mystr/*in*/,char *sub/*in*/, int *ncount)
{
int ret = ;
int tmpCount = ;
//初始化让p指针到查找的条件
char *p = mystr; //不要轻易改变形参的值
if (mystr == NULL || sub == NULL || ncount == NULL)
{
ret = -;
printf("func getCount() err:%d(mystr == NULL || sub || NULL || ncount == NULL)\n", ret);
return ret;
} while (p=strstr(p,sub))
{
tmpCount++;
p = p + strlen(sub);
if (*p=='\0')
break;
}
*ncount = tmpCount; //间接赋值是指针存在的意义
return ret;
} int main()
{
char *p = "abcd1234asd222abcd";
/*char *sub;
sub = "abcd";*/
char sub[] = "abcd";
int ncount=;
int ret = ; ret = getCount(p,sub,&ncount);
if (ret != )
{
printf("func getCount() err:\n", ret); }
printf("ncount:%d\n", ncount); printf("hello...\n");
system("pause");
return ;
}
上一篇:Java学习随笔3:遍历文件夹及文件的读取和写入


下一篇:【 js 基础 】【 源码学习 】 深浅拷贝