strstr函数
char* *strstr(const char *str1,const char *str2)
在str1里查找str2的位置 如果没有则返回NULL
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void main81()
{
char *p = "abcd1111abcd2222abcd333qqq";
char *p2 = NULL;
p2 = strstr(p, "q");
p2 = strchr(p, 'q');//在字符串里查找q第一次出现的位置
if (p2 == NULL)
{
printf("没有找到\n");
}
else
{
printf("找到:%s\n",p2);
}
system("pause");
}
void main82()
{
int count = 0;
char *sub = "abcd";
char *p = "abcd1111abcd2222abcd333qqq";
char *p2 = NULL;
do
{
p = strstr(p, sub);
if (p != NULL)
{
count++;
p = p + strlen(sub);
}
else
{
break;
}
} while (*p != '\0');
printf("count = %d\n", count);
system("pause");
}
void main83()
{
int count = 0;
char *sub = "abcd";
char *p = "abcd1111abcd2222abcd333qqq";
while (p = strstr(p, sub))
{
count++;
p = p + strlen(sub);
if (*p == '\0')
{
break;
}
}
printf("count = %d\n", count);
system("pause");
}
int strstrCount(char *p, char *p2, int *cout) //返回的值也要做成指针形参 不要直接用int类型函数返回
{
int count = 0;
int ret = 0;
if (p == NULL || p2 == NULL || cout == NULL)
{
ret = -1;
return ret;
}
while (p = strstr(p, p2))
{
count++;
p = p + strlen(p2);
if (*p == '\0')
{
break;
}
}
*cout = count;
return 0;
}
void main()
{
int count = 0;
int ret = 0;
char *p2 = "abcd";
char *p = "abcd1111abcd2222abcd333qqq";
ret = strstrCount(p, p2, &count);
if (ret != 0)
{
printf("失败\n");
}
printf("OK\n");
printf("count = %d\n", count);
system("pause");
}