Strrchr Function In C

Syntax:

The strrchr function has the following syntax:

Example

char *strrchr(const char *str, int c);

The strrchr function necessitates two arguments to be provided.

It is a reference to the search string.

It is used to look for in the character.

The strrchr function returns a pointer to the final occurrence of the character c within the string str. In case the character cannot be found, the function will return a null pointer.

Example:

An instance demonstrating the utilization of the strrchr function is provided below:

Example

#include <stdio.h>
#include <string.h>

int main() {
const char *str = "This is a string";
char *last_occurrence = strrchr(str, 's');

if (last_occurrence != NULL) {
printf("The last occurrence of the character 's' in the string is at position %d\n", last_occurrence - str);
} else {
printf("The character 's' was not found in the string\n");
}

return 0;
}

Output:

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 is used to find the last instance of a character within a string. This function can be beneficial in various scenarios, such as identifying the final appearance of a delimiter within a string or the last occurrence of a specific word.

Example: 2 in cases where the string does not contain any occurrences of the specified character:

If the desired character is not present within the input string, the strrchr function will yield a NULL pointer.

Example

#include <stdio.h>
#include <string.h>

int main()
{


	char str[] = "Logic Practice";
	char* ptr;

	
	char chr = 'y';

	
	ptr = strrchr(str, chr);

	
	if (ptr) {
		printf("Last occurrence of %c in %s is at %d", chr,
			str, ptr - str);
	}

	else {
		printf("%c is not present in %s ", chr, str);
	}
	return 0;
}

Output:

Output

y is not present in Logic Practice

There are numerous other standard library functions available for locating characters within strings apart from the strrchr function. These operations consist of:

strchr:

This function searches for a specific character within a string on a single occasion.

strstr:

This function searches for the initial occurrence of a specified substring within a given string.

strpbrk:

This function searches for the initial occurrence of a specified set of characters within a given string.

strcspn:

This function determines the longest prefix length within a string that does not contain any of the specified characters.

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 is valuable for efficiently finding the last occurrence of a character within a string. This function is universally available in the standard C library, ensuring that all C developers can leverage its capabilities for diverse string-manipulation operations.

Input Required

This code uses input(). Please provide values below: