c和指针
本帖最后由 u012453424 于 2013-10-15 18:24:52 编辑 这个程序要求将源字符串里连续的空白字符缩短成一个空白字符,有句话我看不懂
/*
** Shrink runs of white space in the given string to a single space.
*/
#define NUL ’\0’
void
deblank( char *string )
{
char *dest;
char *src;
int ch;
/*
** Set source and destination pointers to beginning of the string, then
** move to 2nd character in string.
*/
src = string;
dest = string++;
/*
** Examine each character from the source string.
*/
while( (ch = *src++) != NUL ){
if( is_white( ch ) ){
/*
** We found white space. If we’re at the beginning of
** the string OR the previous char in the dest is not
** white space, store a blank.
*/
if( src == string/*这里什么意思?注释中的英文我看得懂,就是不懂道理*/|| !is_white( dest[1] ) )
*dest++ = ’ ’;
}
else {
/*
** Not white space: just store it.
*/
*dest++ = ch;
}
}
*dest = NUL;
}
int
is_white( int ch )
{
return ch == ’ ’ || ch == ’\t’ || ch == ’\v’ || ch == ’\f’ || ch == ’\n’
|| ch == ’\r’;
}
[解决办法]
你自己动手画一画,如果第一个就是就为空呢?
[解决办法]
那是处理开始就是空格,如果没有这句,dest会访问越界的。其他的就是如果不是空格就直接赋给dest,如果是空格,就判断dest前一个是否是空格了,如果不是,赋值空格,如果前一个也是空格,跳过
[解决办法]
程序是调试出来的