Javatpoint Logo
Javatpoint Logo

Strrchr() function in C

Introduction:

The strrchr() method in the C standard library looks for the final instance of a given character within a string. The function returns a pointer to the final instance of the character in the string. The function returns a null pointer if the character cannot be located. The string.h header file contains a declaration for the strrchr() function.

Syntax:

The strrchr() function has the following syntax:

Two arguments are required by the strrchr() method.

str:

It is a reference to the search string.

c:

It is used to look for in the character.

A pointer to the last instance of the character c in the string str is returned by the strrchr() function. The function returns a null pointer if the character cannot be located.

Example:

An example of how to use the strrchr() function is as follows:

Output:

The following is the program's output:
The last occurrence of the character 's' in the string is at position 10

Explanation:

The strrchr() function can be utilized to locate a character's final occurrence inside a string. It can be helpful for a number of purposes, like locating the final occurrence of a delimiter in a string or the final occurrence of a certain term.

Example: 2 when the string contains no instances of the character:

If the character being searched for cannot be found in the input string, the strrchr() function returns a NULL pointer.

Output:

y is not present in JavaTpoint

There are several more standard library functions that can be used to search for characters in strings in addition to the strrchr() method. These activities include are:

strchr():

This function looks for a particular character in a string only once.

strstr():

This function looks for a given substring's first occurrence within a string.

strpbrk():

This function looks for a string's first instance of a provided collection of characters.

strcspn():

This function calculates the length of the longest prefix in a string that doesn't include any of the characters you specify.

strspn():

The longest prefix of a string that solely comprises characters from a set of specified characters is returned by this method. Several third-party libraries offer more string-searching functionality in addition to the standard library functions. These libraries can be employed to carry out more difficult string-searching tasks, like regular expression matching.

Conclusion:

The strrchr() function can be used to locate a character's final appearance in a string quickly and easily. Since the function is a part of the standard library, every C implementation has access to it. Any C programmer can benefit from the strrchr() function, which can be used to carry out a variety of string-searching tasks.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA