C Reference String Operation: strrchr()

The function strrchr() returns a pointer to the last occurrence of x in a string.

Usage:

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

Note: NULL will be returned if x isn’t found.

strrchr() source code example:


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

	main()
	{
		char line[100];
		char *ptr_my;

		printf("Input word:");
		scanf("%s", line);

		ptr_my=strrchr(line, 'a');

		if (ptr_my == NULL)
			printf("Character a is not found.\n");
		else
			printf("Character found at %d\n", ptr_my - line+1);
	}

Output of the example:


	Input word:testen
	Character a is not found.

	Input word:tastan
	Character found at position 5

Note: the program was executed two times to get the result of the output example.

This entry was posted in C Reference string.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Reference String Operation: strrchr()”

Why not let us know what you think by adding your own comment!

  1. Hasan on December 13th, 2010:

    there is a diffirence between strchr and strrchr
    strchr
    The function strchr() returns a pointer to the first occurrence of x in a string.

    The function strrchr() returns a pointer to the last occurrence of x in a string.

  2. admin on December 17th, 2010:

    @Hasan Thx for pointing that out. The problem was that the title of this page was missing a r in the functions name. Corrected the problem. The string operation strchr() can be found here

Leave a Reply: