C Reference String Operation: strchr()

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

Usage:

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

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

strchr() source code example:


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

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

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

	ptr_my=strchr(line, 'a');

	if (ptr_my == NULL)
		printf("Character a is not found.\n");
	else
	printf("Character found at position %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 2

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: