Chinaunix首页 | 论坛 | 博客
  • 博客访问: 383930
  • 博文数量: 55
  • 博客积分: 1907
  • 博客等级: 上尉
  • 技术积分: 869
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-04 19:30
文章分类

全部博文(55)

文章存档

2011年(32)

2010年(23)

分类: C/C++

2010-11-28 19:05:22

/*
 * 如果字符串 "substr" 出现在 "str" 中,则在 "str" 中删除它。
 * If the string "substr" appears in "str", delete it.
 
 * 2010-11-20
 * got it
 */
 

#define NULL    0        /* null pointer */
#define NUL        '\0'    /* null byte */
#define TRUE    1        
#define FALSE    0    

/*
 *    see if the substring beginning at 'str' matches the string 'want'.
 *    if so,return a pionter to the first character in 'str' after the match.
 */

char *match( char *str, char *want )
{
    /*
     * Keep looking while there are more characters in 'want'.
     * we fall out of the loop, if we get a match.
     */

    while( *want!=NUL )
    {
        if( *str++ != *want++ )
            return NULL;
    }
    return str;        
}    


int del_substr( char *str, char const *substr )
{
    char *next;

    while( *str != NUL )
    {
        next = match( str,substr );
        if( next != NULL )    
            break;            
        str++;
    }

    if( *str == NUL )
        return FALSE;

    while( *str++ == *next++ )
        ;
    return TRUE;
}


阅读(1256) | 评论(0) | 转发(0) |
0

上一篇:指针之6.1

下一篇:指针之6.3

给主人留下些什么吧!~~