strchr

 

Description

Find the first occurrence of a character in a string.

Syntax

LPSTR strchr( LPCSTR lpcszStr, int cc )

Parameters

lpcszStr
[input] NULL terminated source string
cc
[input] Character to be located

Return

Returns a pointer to the first occurrence of a character in a string.

Examples

EX1

void strchr_ex1()
{
        int  ch = 'O';
        char str[] = "Welcome to use Origin!";
        printf( "String to be searched: \t%s\n", str );
        printf( "Search char:\t%c\n", ch );
        
        /* Search forward. */
        char *pCh = strchr( str, ch ); // to search 'O', if found return the position
        printf("%s\n", pCh); // output "Origin!"
        
        if( pCh != NULL )
        {
                int nPos = pCh - str + 1;
                printf( "Result:\tFirst %c found at position %d\n\n", ch, nPos );
        }
        else
                printf( "Result:\t%c not found\n" );
}

Remark

See Also

strrchr

Header to Include

origin.h

Reference