我有这段代码:
int i =0;
char * str = "ar bitrary whitespace";
int whitespace=0,index;
for(index = 0;index < strlen(str);index++)
{
if(isspace(str[index]) != 0)
{
whitespace++;
}
}
char * tmp = (char *)calloc(strlen(str)-whitespace +1,sizeof(char));
memset(tmp,'\0',strlen(tmp)+1);
while(i < strlen(str))
{
if(isspace(str[i]) != 0)
{
i++;
continue;
}else if(isspace(str[i]) == 0)
{
strcat(tmp,&str[i]);
i++;
}
}
printf("\nnew string is: %s \n",tmp);
问题是输出是一个没有删除空格的字符串+一些垃圾字符。 我已经使用 memset 来 null 终止 tmp,有问题吗?
最佳答案
源字符串的长度可以在此循环之前计算
for(index = 0;index < strlen(str);index++)
否则,如果代码不会被优化,函数 strlen
将在循环的每次迭代中被调用。事实上,使用该函数对于这样的任务来说是多余的。
这个声明
memset(tmp,'\0',strlen(tmp)+1);
没有意义,因为 calloc
的调用已经用零初始化了内存。
这个声明
strcat(tmp,&str[i]);
还在位置 i
之后复制源字符串中的空格。因此它可以写入超出为指针 tmp
指向的数组分配的内存。
您可以编写一个单独的函数,看起来就像这个演示程序中显示的那样
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
char * remove_blanks( const char *s )
{
size_t n = 0;
const char *p = s;
do
{
if ( !isspace( ( unsigned char )*p ) ) ++n;
} while ( *p++ );
char *t = malloc( n );
if ( t )
{
char *q = t;
p = s;
do
{
if ( !isspace( ( unsigned char )*p ) ) *q++ = *p;
} while ( *p++ );
}
return t;
}
int main(void)
{
char * str = "ar bitrary whitespace";
printf( "\"%s\"\n", str );
char *t = remove_blanks( str );
printf( "\"%s\"\n", t );
free( t );
}
程序输出为
"ar bitrary whitespace"
"arbitrarywhitespace"
关于c - 删除空格后的垃圾输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42374286/