< Prev
Next >



C++ strchr() function




As per the name of the function goes, strchr() function is one of the string handling library functions. The function strchr() is used to search for a particular char value in the target string(stored in a c-style char array). Both the char value and string is passed to this function. Let' see the signature of this function.



Signature of strchr()


char *strchr(const char *str, int c)

Looking at the signature of strchr() function, it is passed two paramaters when it is called, where -





Here in the next code, we are going to use strchr() function to search a string for a char value with multiple occurrences and we will see what happens in this case.
// Program to find a character in a string, with multiple occurrences


#include<iostream>
#include<cstring>

using namespace std;

int main()
{
char arr[] = "a beautiful sky";
char ch = 'e';

char *p = strchr(arr,ch); // it prints the string after the searched char value
 
cout<< "String to be searched for a value : " << arr << "\n";
cout<< "char value to be searched in String : " << ch << "\n";
cout<< "Result of search : " << p;

return 0;
}


Output is :


String to be searched for a value : a beautiful sky
char value to be searched in String : e
Result of search : eautiful sky


Program Analysis





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< C-Style Characters Strings
C++ strlen() function >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2