詳解C語言中strpbrk()函數(shù)的用法
頭文件:
#include <include.h>
strpbrk()函數(shù)檢索兩個(gè)字符串中首個(gè)相同字符的位置,其原型為:
char *strpbrk( char *s1, char *s2);
【參數(shù)說明】s1、s2要檢索的兩個(gè)字符串。
strpbrk()從s1的第一個(gè)字符向后檢索,直到'\0',如果當(dāng)前字符存在于s2中,那么返回當(dāng)前字符的地址,并停止檢索。
【返回值】如果s1、s2含有相同的字符,那么返回指向s1中第一個(gè)相同字符的指針,否則返回NULL。
注意:strpbrk()不會(huì)對(duì)結(jié)束符'\0'進(jìn)行檢索。
【函數(shù)示例】輸出第一個(gè)相同字符之后的內(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:實(shí)現(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)文章
C++結(jié)構(gòu)體與類指針知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了關(guān)于C++結(jié)構(gòu)體與類指針知識(shí)點(diǎn)以及相關(guān)內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。2019-09-09OpenCV數(shù)字圖像處理基于C++之圖像形態(tài)學(xué)處理詳解
OpenCV是一款由Intel公司俄羅斯團(tuán)隊(duì)發(fā)起并參與和維護(hù)的一個(gè)計(jì)算機(jī)視覺處理開源軟件庫,支持與計(jì)算機(jī)視覺和機(jī)器學(xué)習(xí)相關(guān)的眾多算法,下面這篇文章主要給大家介紹了關(guān)于OpenCV數(shù)字圖像處理基于C++之圖像形態(tài)學(xué)處理的相關(guān)資料,需要的朋友可以參考下2022-12-12