wcsrchr的妙用
该函数用于在宽字符串中查找某一个字符(c),并返回对应该字符的地址
/*************介绍下该函数***************/
字符串中字符查找函数:strchr,wcschr 及strrchr, wcsrchr函数
Win CE 2009-11-12 13:50:12 阅读328 评论0 字号:大中小
(1)
char *strchr( const char *string, int c );
wchar_t *wcschr( const wchar_t *string, wchar_t c );
Find a character in a string.
查找一个字符串中首次出现的指定字符。
Return Value
Each of these functions returns a pointer to the first occurrence of c in string(address), or NULL if c is not found.
(2)
char *strrchr( const char *string, int c);
char *wcsrchr( const wchar_t *string, int c );
Scan a string for the last occurrence of a character.
查找一个字符串中最后出现的指定字符。
Return Value
Each of these functions returns a pointer to the last occurrence of c in string(address), or NULL if c is not found.
找出字符串中最后一个出现查找字符的地址,然后将该字符出现的地址返回。
/*********************接下来讲述一个例子****************************/
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
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nicolas16/archive/2007/11/05/1868277.aspx