|
发表于 2010-3-18 13:45:33
|
显示全部楼层
Post by 小锐同学;2076136
/*
** If the string "substr" appears in "str", delete 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 pointer to the first character in ’str’ after the match.
*/
char *
match( char *str, char *want )
{
/*
** 把*str跟want中的字符比较直到want完结
*/
while( *want != NUL )
if( *str++ != *want++ )
return NULL;
return str;
}
int
del_substr( char *str, char const *substr )
{
char *next;
/*
** Look through the string for the first occurrence of the substring.
*/
while( *str != NUL )
{
next = match( str, substr );
if( next != NULL )
break;
str++;
}
/*
** If we reached the end of the string, then the substring was not
** found.
*/
if( *str == NUL )
return FALSE;
/*
** Delete the substring by copying the bytes after it over the bytes of
** the substring itself.
*/
while( *str++ = *next++ )//这里,以什么作为跳出while循环的条件?--------------------------------??
;
return TRUE;
}
当*next为0的时候,为\0的时候也是为0的时候么。
谷歌说:计算C风格字符串长度一定要算上终结符null
记得NULL就是为0。
那这里的\0跟0是什么关系。
谢谢。
'\0' == 0
这两者相等。但是一个有引号,一个没有引号。
另外,NULL 在 C++ 中就是 0,在 C 中,NULL 是 (void *)0,这两者有区别,C++的作者说过。 |
|