strchr – Find Character In A String C and C++ Tutorial with Examples – POFTUT

strchr – Find Character In A String C and C++ Tutorial with Examples

C library provides a lot of functions in order to use string or char array types. strchr() function is a very popular function which is used to find the first occurrence of a given character in a string or char array.

Syntax and Parameters

As strchr() provides the first occurrence of the given char it will return a pointer to the first occurrence.  We will also provide the string or char array we are searching in and the chart we want to locate.

  • const char* type is the string or char array we are searching in
  • int is the char we are searching for value

Return Value

The return value is a char pointer to the first occurrence of the given char .

Example with C

We will start with a C example where we will search the s character in the string named str.

We will compile with the following gcc command.

LEARN MORE  Python String Variable Type

and call the example executable strchr_C_example.

Example with C

Example with C

Example with C++

As stated previously strchr() function exist in C++ programming language standard library. It has the same syntax where provided by std library as a static function.

We will compile an example with the following g++ command.

and then we will call created example binary strchr_Cpp_example

Example with C++

Example with C++

Leave a Reply

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

Enjoy this blog? Please spread the word :)