查找字符串子串个数

include “stdio.h”

include “string.h”

int count_str(char p, char sub, int *num)
{
int temp = 0;
char *temp_p = p;
if (p == NULL || sub == NULL || num == NULL)
return -1;
while (*temp_p != ‘\0’)
{
temp_p = strstr(temp_p, sub);
if (temp_p != NULL)
{
temp_p = strlen(sub) + temp_p;
temp++;
}
else{
break;
}
}

*num = temp;
return 1;

}
void main()
{
char *p = “sdfas123d123aa123aa”;
char *p1 = “123”;
int num = 0;
count_str(p, p1, &num);

printf("%d", num);

}

猜你喜欢

转载自blog.csdn.net/sunt1921/article/details/80327605