strrchr, wcsrchr, _mbsrchr

strrchr, wcsrchr, _mbsrchr

Scan a string for the last occurrence of a character.

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

char *wcsrchr( const wchar_t *string, int c );

int _mbsrchr( const unsigned char *string, unsigned int c );

RoutineRequired HeaderCompatibility
strrchr<string.h>ANSI, Win 95, Win NT
wcsrchr<string.h> or <wchar.h>ANSI, Win 95, Win NT
_mbsrchr<mbstring.h>Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns a pointer to the last occurrence of c in string, or NULL if c is not found.

Parameters

string

Null-terminated string to search

c

Character to be located

Remarks

The strrchr function finds the last occurrence of c (converted to char) in string. The search includes the terminating null character.

wcsrchr and _mbsrchr are wide-character and multibyte-character versions of strrchr. The arguments and return value of wcsrchr are wide-character strings; those of _mbsrchr are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined_MBCS Defined_UNICODE Defined
_tcsrchrstrrchr_mbsrchr wcsrchr

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 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值