【发布时间】:2020-11-12 15:36:41
【问题描述】:
我正在尝试从字符串中删除给定的字符,并且大部分情况下我的代码都在工作,但是当我将“”指定为字符串并将“”指定为字符时,我的断言失败了。
#include <stdio.h>
#include <string.h>
#include <assert.h>
void strrem(const char* s1, const char c, char* s2);
int main(void)
{
char str[1000];
strrem("abc", 'b', str); assert(strcmp(str,"ac")==0);
strrem("ABC", 'a', str); assert(strcmp(str,"ABC")==0);
strrem("Hello World!", '!', str); assert(strcmp(str,"Hello World")==0);
strrem("", ' ', str); assert(strcmp(str,"")==0);
return 0;
}
void strrem(const char* s1, const char c, char* s2)
{
int i, j;
int strlngth = strlen(s1);
for (i = 0, j = 0; i < strlngth; i++) {
if(s1[i] != c) {
s2[j++] = s1[i];
}
s2[j] = '\0';
}
}
【问题讨论】: