wcsrchr的妙用

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

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值