詳解C語言中strpbrk()函數(shù)的用法
頭文件:
#include <include.h>
strpbrk()函數(shù)檢索兩個字符串中首個相同字符的位置,其原型為:
char *strpbrk( char *s1, char *s2);
【參數(shù)說明】s1、s2要檢索的兩個字符串。
strpbrk()從s1的第一個字符向后檢索,直到'\0',如果當(dāng)前字符存在于s2中,那么返回當(dāng)前字符的地址,并停止檢索。
【返回值】如果s1、s2含有相同的字符,那么返回指向s1中第一個相同字符的指針,否則返回NULL。
注意:strpbrk()不會對結(jié)束符'\0'進行檢索。
【函數(shù)示例】輸出第一個相同字符之后的內(nèi)容。
#include<stdio.h>
#include<string.h>
int main(void){
char* s1 = "http://see.xidian.edu.cn/cpp/u/xitong/";
char* s2 = "see";
char* p = strpbrk(s1,s2);
if(p){
printf("The result is: %s\n",p);
}else{
printf("Sorry!\n");
}
return 0;
}
輸出結(jié)果:
The result is: see.xidian.edu.cn/cpp/u/xitong/
DEMO:實現(xiàn)自己的strpbrk函數(shù)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#pragma warning (disable:4996)
char *mystrpbrk(const char *cs,const char *ct);
int main(void)
{
char *s1="Welcome to Beijing.";
char *s2="BIT";
char *s3;
s3=mystrpbrk(s1,s2);
printf("%s\n",s3);
getch();
return 0;
}
/*FROM 百科*/
char *mystrpbrk(const char *cs,const char *ct)
{
const char *sc1,*sc2;
for (sc1=cs;*sc1!='\0';sc1++)
{
for (sc2=ct;*sc2!='\0';sc2++)
{
if (*sc1==*sc2)
{
return (char *)sc1;
}
}
}
return NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#pragma warning (disable:4996)
int main(void)
{
char *s1="Welcome to Beijing.";
char *s2="BIT";
char *p;
system("cls");
p=strpbrk(s1,s2);
if (p)
{
printf("%s\n",p);
}
else
{
printf("NOT Found\n");
}
p=strpbrk(s1,"i");
if (p)
{
printf("%s\n",p);
}
else
{
printf("NOT Found\n");
}
getch();
return 0;
}
相關(guān)文章
Java?C++?算法題解leetcode652尋找重復(fù)子樹
這篇文章主要為大家介紹了Java?C++?算法題解leetcode652尋找重復(fù)子樹示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Pipes實現(xiàn)LeetCode(192.單詞頻率)
這篇文章主要介紹了Pipes實現(xiàn)LeetCode(192.單詞頻率),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
c++動態(tài)內(nèi)存空間示例(自定義空間類型大小和空間長度)
這篇文章主要介紹了c++動態(tài)內(nèi)存空間示例,自定義空間類型大小和空間長度,需要的朋友可以參考下2014-04-04
關(guān)于C++復(fù)制構(gòu)造函數(shù)的實現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++復(fù)制構(gòu)造函數(shù)的實現(xiàn)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
C++ Log日志類輕量級支持格式化輸出變量實現(xiàn)代碼
這篇文章主要介紹了C++ Log日志類輕量級支持格式化輸出變量實現(xiàn)代碼,需要的朋友可以參考下2019-04-04

