Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

strpbrk in C language

by suresh

The C strpbrk function is a String Function, which used to find the first character in a first string that matches any character in a second string. The syntax of the strpbrk in C Programming language is

char *strpbrk(const char *str1, const char *str2);

or we can simply write strpbrk as:

strpbrk(str1, str2);

strpbrk in C language Example

This program will help you to understand the strpbrk with multiple examples.

TIP: You have to include the #include<string.h> header before using this strpbrk String Function.

/* C strpbrk Function example  */

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50] = "abcdcefgdhiejk";
   	char str2[50] = "ce";
   	char *result;
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}

OUTPUT

strpbrk in C language Example 1

strpbrk in C Example 2

Instead of predefined the two strings, this C program allows the user to enter string 1 and string 2. Next, C Program will find first records from string 1 that matches any character in string 2

/* C strpbrk Function example  */

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50], str2[50];
   	char *result;
	
	printf("\n Please Enter any String  : ");
	gets(str1);	
	
	printf("\n Please Enter the String that you want to Match : ");
	gets(str2);	
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}

OUTPUT

strpbrk in C language Example 2

Although all the four characters in the second string exist in str1, It returned the output as l. It is because l is the first occurrence letter in string 1 compared to e y d

Placed Under: C Programming

  • C Comments
  • C Escape Sequence
  • C Programming Operators
  • C Arithmetic Operators
  • C Assignment Operators
  • C Bitwise Operators
  • C Conditional Operator
  • C Increment & Decrement Optr
  • C Logical Operators
  • C Relational Operators
  • C Sizeof Operator
  • C If Statement
  • C IF ELSE Statement
  • C Else If Statement
  • C Nested If
  • C Break Statement
  • C Continue Statement
  • C Goto Statement
  • C Switch Case
  • C For Loop
  • C While Loop
  • C Do While Loop
  • C Arrays
  • C Two Dimensional Array
  • C Multi Dimensional Array
  • C String
  • C Structures
  • C Nested Structures
  • C Array of Structures
  • C Structures and Functions
  • C Union
  • C Structure & Union differences
  • C Pointers
  • C Pass Pointers to Functions
  • C GETS
  • C fgets Function
  • C fgetc
  • C fputs function
  • C fputc
  • C Functions
  • C Types of Functions
  • C Pass Array to Functions
  • Call By Value & Call By Reference
  • C Recursion
  • C Program Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy