加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

详解C语言中strpbrk()函数的用法

发布时间:2020-12-16 03:03:55 所属栏目:百科 来源:网络整理
导读:头文件: #include include.h strpbrk()函数检索两个字符串中首个相同字符的位置,其原型为: char *strpbrk( char *s1,char *s2); 【参数说明】s1、s2要检索的两个字符串。 strpbrk()从s1的第一个字符向后检索,直到'',如果当前字符存在于s2中,那么返回

头文件:

#include <include.h>

strpbrk()函数检索两个字符串中首个相同字符的位置,其原型为:

  char *strpbrk( char *s1,char *s2);

【参数说明】s1、s2要检索的两个字符串。

strpbrk()从s1的第一个字符向后检索,直到'',如果当前字符存在于s2中,那么返回当前字符的地址,并停止检索。

【返回值】如果s1、s2含有相同的字符,那么返回指向s1中第一个相同字符的指针,否则返回NULL。

注意:strpbrk()不会对结束符''进行检索。

【函数示例】输出第一个相同字符之后的内容。

#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: %sn",p);  
  }else{
    printf("Sorry!n");
  }
  return 0;
}

输出结果:

The result is: see.xidian.edu.cn/cpp/u/xitong/

DEMO:实现自己的strpbrk函数

#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("%sn",s3);
 getch();
 return 0;
}
/*FROM 百科*/
char *mystrpbrk(const char *cs,const char *ct)
{
 const char *sc1,*sc2;
 for (sc1=cs;*sc1!='';sc1++)
 {
 for (sc2=ct;*sc2!='';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("%sn",p);
 }
 else
 {
 printf("NOT Foundn");
 }
 p=strpbrk(s1,"i");
 if (p)
 {
 printf("%sn",p);
 }
 else
 {
 printf("NOT Foundn");
 }
 getch();
 return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读