strpbrk() – String Manipulation Function

strpbrk() – String Manipulation Function

How to replace a character in C++?

Explanation

strpbrk() manipulation function is used to replace by returning a pointer to the first character in str1 that matches any character in the “str2”. Null terminators are not included and it returns a null pointer if no matches are found.

Syntax:

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

Example :

#include
#include
int main(void)
{
char str1[]=”Hi welcome to hscripts!”;
char *p;
while((p=strpbrk(str1,” “))!=NULL)
*p=’*’;
printf(“The String with * replacing
spaces is:: %s.n”,str1);
return 0;
}

Result :

The String with * replacing spaces is:: Hi*welcome*to*hscripts!.

In the above example, strpbrk() manipulation function is used to replace the space in “str1” with “*”.





Leave a Reply

Your email address will not be published. Required fields are marked *