标题: strpbrk函数实现及简单使用 [打印本页]

作者: xiaojuan    时间: 2014-9-24 22:24
标题: strpbrk函数实现及简单使用
//:Vc++6.0  String  strpbrk函数
//功能:顺序在字符串str1中搜寻与str2中字符的第一个相同字符
//参数:str1 字符串1   str2 字符串2
//返回值:返回找到字符串的字符
#include<stdio.h>

const char *strpbrk(const char *str1, const char *str2);

int main()
{

char str[] = "This is a sample string";
char key[] = "aeiou";
const char * pch;
printf ("Vowels in '%s': ",str);
pch = strpbrk(str, key);

while (pch != NULL)
{
printf ("%c " , *pch);
pch = strpbrk(pch+1,key);
}
printf ("\n");

return 0;
}

const char *strpbrk(const char *str1, const char *str2)
{
if (str1 == NULL || str2 == NULL)
{
perror("str1 or str2");
return NULL;
}
const char *temp1 = str1;
const char *temp2 = str2;

while (*temp1 != '\0')
{
temp2 = str2; //将str2 指针从新指向在字符串的首地址
while (*temp2 != '\0')
{
if (*temp2 == *temp1)
return temp1;
else
temp2++;
}
temp1++;
}
return NULL;
}

//在vc++6.0中的运行结果为:Vowels in 'This is a sample string': i i a a e i
//注:和 strcspn函数 相似//:~






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1