Details the use of STRPBRK of function in C language

  • 2020-04-02 03:17:03
  • OfStack

The header file:


#include <include.h>

The STRPBRK () function retrieves the position of the first identical character in two strings, and the prototype is:


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

[parameter description] two strings to be retrieved by s1 and s2.

STRPBRK () retrieves the first character of s1 backwards until '\0', returns the address of the current character if it exists in s2, and stops the retrieval.

[return value] if s1 and s2 contain the same character, then return a pointer to the first same character in s1, otherwise return NULL.

Note: STRPBRK () does not retrieve the ending '\0'.

Output after the first identical character.


#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;
}

Output results:


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

DEMO: implement your STRPBRK function


#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;
}

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("%sn",p);
 }
 else
 {
 printf("NOT Foundn");
 }
 p=strpbrk(s1,"i");
 if (p)
 {
 printf("%sn",p);
 }
 else
 {
 printf("NOT Foundn");
 }
 getch();
 return 0;
}


Related articles: