strchr, _fstrchr

Description

Find a character in a string.

#include <string.h> Required only for function declarations  

char *strchr( const char *string, int c );

char __far * __far _fstrchr( const char __far *string, int c );

string Source string  
c Character to be located  

Remarks

The strchr and _fstrchr functions return a pointer to the first occurrence of c (converted to char) in string. The converted character c may be the null character ('\0'); the terminating null character of string is included in the search. The function returns NULL if the character is not found.

The strchr and _fstrchr functions operate on null-terminated strings. The string arguments to these functions are expected to contain a null character ('\0') marking the end of the string.

The _fstrchr function is a model-independent (large-model) form of the strchr function. The behavior and return value of _fstrchr are identical to those of the model-dependent function strchr, with the exception that the arguments and return values are far.

Return Value

The return values for these functions are described above.

Compatibility

strchr

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_fstrchr

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

strcspn, strncat, strncmp, strncpy, _strnicmp, strpbrk, strrchr, strspn, strstr

Example

/* STRCHR.C: This program illustrates searching for a character with

* strchr (search forward) or strrchr (search backward).

*/

#include <string.h>

#include <stdio.h>

int ch = 'r';

char string[] = "The quick brown dog jumps over the lazy fox";

char fmt1[] = " 1 2 3 4 5";

char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )

{

char *pdest;

int result;

printf( "String to be searched: \n\t\t%s\n", string );

printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );

printf( "Search char:\t%c\n", ch );

/* Search forward. */

pdest = strchr( string, ch );

result = pdest - string + 1;

if( pdest != NULL )

printf( "Result:\tfirst %c found at position %d\n\n", ch, result );

else

printf( "Result:\t%c not found\n" );

/* Search backward. */

pdest = strrchr( string, ch );

result = pdest - string + 1;

if( pdest != NULL )

printf( "Result:\tlast %c found at position %d\n\n", ch, result );

else

printf( "Result:\t%c not found\n" );

}

Output

String to be searched:

The quick brown dog jumps over the lazy fox

1 2 3 4 5

12345678901234567890123456789012345678901234567890

Search char: r

Result: first r found at position 12

Result: last r found at position 30