C++ strchr()

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

A string in C++ is a type of object that represents a collection (or sequence) of different characters… With the help of the c++ strchr() function, we can check whether a character is present in the string or not. The strchr() function in c++ is present in the <cstring> standard library file.

Syntax of C++ strchr()

The syntax of the strchr() function in C++ is as follows.

Alternatively, the C++ strchr() function can have a syntax as follows.

Parameters of C++ strchr()

The C++ strchr() function takes 2 parameters that are, str and ch.

  • str: The string in which the character has to be searched.
  • ch: The character which needs to be searched in the string, but it is passed in the form of an integer which is its ASCII value.

Return Value of C++ strchr()

The c++ strchr() function returns a pointer pointing to the first location of the character (ch) in the string (str) which is being searched if the character is found in the string. A null pointer is returned if the character is not found in the string.

Exceptions of C++ strchr()

If the passed arguments have the correct data type then this function never throws any exception.

What is C++ strchr()?

The strchr() is a case-sensitive function defined under the <cstring> header file is an inbuilt function in C++ standard library. C++ strchr() is used to search for a character in a given string. The function takes the character to be searched and the string in which the character has to be searched as the parameter.

The strchr() function casts the value of ch from int to char internally. The function checks if the character is present in the string or not. If the character is present in the string, then a pointer pointing to the first occurrence of the character in the string is returned. If not a NULL pointer is returned by the function.

Example

We will make use of the strchr() function to check whether the character is present in the string or not.

output

Explanation

In the above code,

  • The variable str stores the string to be searched, and ch stores the character to be searched in the string str.
  • str and ch are passed as the parameters to the c++ strchr() function.
  • Character 'ch' that is 'l' here was present in the string so the pointer pointing to its position was returned and the print statement of if-statement is printed.

Examples

Example1:

We will make use of the strchr() function to find the index of the first occurrence of a character in the string.

output

Explanation

In the above code,

  • The string in which the character has to be searched is stored in the string str and the character to be searched is stored in the variable ch.
  • str and ch are passed as the parameter to the c++ strchr() function.
  • The pointer returned by the c++ strchr() function is stored in the pointer result.
  • Later, the pointer returned by the function is not equal to NULL so, the index of the first occurrence of the character in the string is printed using result-str.

Example2:

C++ program to show that the strchr() function is case sensitive.

output

Explanation

  • In the above code, we are searching for the character t in the string Scaler Topics.
  • It is evident that t is present in "Scaler Topics", but the output says that the character is not present in the string.
  • This is because the search string and character to be searched are treated as case-sensitive.

Conclusion

  • The c++ strchr() function is included under the <cstring> standard c++ library.
  • The syntax of c++ strchr() function is char* strchr( char* str, int ch );.
  • The strchr() function in c++ takes str (The string in which the character has to be searched ) and ch (The character to be searched in the string) as the parameters.
  • The strchr() function returns a pointer pointing to the first occurrence of the character (ch) in the string (str).
  • If the character (ch) is not found in the string (str), a null pointer is returned by the function.

See also