找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2523|回復(fù): 0
收起左側(cè)

strpbrk函數(shù)實(shí)現(xiàn)及簡單使用

[復(fù)制鏈接]
ID:51090 發(fā)表于 2014-9-24 22:24 | 顯示全部樓層 |閱讀模式
//:Vc++6.0  String  strpbrk函數(shù)
//功能:順序在字符串str1中搜尋與str2中字符的第一個(gè)相同字符
//參數(shù):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中的運(yùn)行結(jié)果為:Vowels in 'This is a sample string': i i a a e i
//注:和 strcspn函數(shù) 相似//:~

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表