strstr

  1   /*    
      2     *   strstr.c   --  
      3     *  
      4     *             Source   code   for   the   " strstr"   library   routine.  
      5     *  
      6     *   Copyright   (c)   1988-1993   The   Regents   of   the   University   of   California.  
      7     *   Copyright   (c)   1994   Sun   Microsystems,   Inc.  
      8     *  
      9     *   See   the   file   "license.terms"   for   information   on   usage   and   redistribution  
    10     *   of   this   file,   and   for   a   DISCLAIMER   OF   ALL   WARRANTIES.  
    11     *  
    12     *   RCS:   @(#)   $Id:   strstr.c,v   1.1.1.2   2001/07/09   18:46:58   lim   Exp   $  
    13     */  
    14    
    15   /*  
    16     *----------------------------------------------------------------------  
    17     *  
    18     *   strstr   --  
    19     *  
    20     *             Locate   the   first   instance   of   a   substring   in   a   string.  
    21     *  
    22     *   Results:  
    23     *             If   string   contains   substring,   the   return   value   is   the  
    24     *             location   of   the   first   matching   instance   of   substring  
    25     *             in   string.     If   string   doesn't   contain   substring,   the  
    26     *             return   value   is   0.     Matching   is   done   on   an   exact  
    27     *             character-for-character   basis   with   no   wildcards   or   special  
    28     *             characters.  
    29     *  
    30     *   Side   effects:  
    31     *             None.  
    32     *  
    33     *----------------------------------------------------------------------  
    34     */  
    35    
    36   char   *  
    37   strstr(string,   substring)  
    38           register   char   *string;             /*   String   to   search.   */  
    39           char   *substring;                         /*   Substring   to   try   to   find   in   string.   */  
    40   {  
    41           register   char   *a,   *b;  
    42    
    43           /*   First   scan   quickly   through   the   two   strings   looking   for   a  
    44             *   single-character   match.     When   it's   found,   then   compare   the  
    45             *   rest   of   the   substring.  
    46             */  
    47    
    48           b   =   substring;  
    49           if   (*b   ==   0)   {  
    50                   return   string;  
    51           }  
    52           for   (   ;   *string   !=   0;   string   +=   1)   {  
    53                   if   (*string   !=   *b)   {  
    54                           continue;  
    55                   }  
    56                   a   =   string;  
    57                   while   (1)   {  
    58                           if   (*b   ==   0)   {  
    59                                   return   string;  
    60                           }  
    61                           if   (*a++   !=   *b++)   {  
    62                                   break;  
    63                           }  
    64                   }  
    65                   b   =   substring;  
    66           }  
    67           return   (char   *)   0;  
    68   }  
    69     
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值