C Reference String Operation: strchr()

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

Usage:

char *strchr( 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.

Leave a Reply: