In C ++, STRCHR () is a predefined function that looks for the occurrence of a character in a string. It exists in the Cstring header file.

syntactic

// cchar * STRCHR (const char * STR, int c) in STR []

Notice that c is passed as its int promotion, but internally treated as a char.

The application

In c ++, given a string, we need to find the first occurrence of a character, let’s say ‘a’.

Example:

Input: STR [] =’ this is a string ‘output: 9 Input: STR [] =’ my name is Ayush’ output 4

algorithm

1. Pass the given string in the STRCHR () function and mention the character you need to point to.

2. The function returns a value and prints the value.

The following is the implementation of the above algorithm:

Output: 5

The STRCHR () function can also be used to check for the presence of characters in strings. The input contains the character to check if it exists in a string.

For example — let’s check for characters A and Z in the string — “My name is Ayush”

algorithm

1. Pass the character as the second argument to STRCHR () in the given string and check that the returned value is not null.

2. If the function returns NULL, it indicates that the string does not contain the character, so print the required statement.

3. Otherwise, if the function does not return NULL, it means that the string contains characters, so print the required statement.

The following is the implementation of the above algorithm:

Output: A exists in the string, z does not exist in the string

The STRCHR () function can be used to find the absolute directory path of Linux:

Example:

Input: /home/test/sample Output: /home/test

Algorithm:

Find the location of the last “/” in the directory path by using STRRCHR.

Replaces the present content with NULL characters.

The following is the implementation of the above algorithm:

Output: /home/test/sample (contains NULL characters)

Every day to learn a little knowledge, I hope to help you ~

In addition, if you want to improve your programming ability, learn C language C++ programming! Pass on the curve, one step faster! The author here may be able to help you ~

C language C++ programming learning exchange circle, **QQ group [464501141] ** wechat public number: C language programming learning base

Share (source code, project actual combat video, project notes, basic introduction tutorial)

Welcome to change careers and learn programming partners, use more information to learn and grow faster than their own thinking oh!

Programming learning books:

Programming learning video sharing: