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);
}
本文介绍了一个用C语言编写的函数,该函数用于计算一个字符串中特定子串出现的次数,并展示了如何调用此函数进行实际应用。
1028

被折叠的 条评论
为什么被折叠?



