是否有任何 C 库函数可以将char
数组(包含一些'\0'
字符)复制到另一个char
数组,而不复制'\0'
?
例如,"he\0ll\0o"
应复制为"hello"
.
问问题
1321 次
3 回答
3
只要您知道 char 数组的长度:
void Copy(const char *input, size_t input_length, char *output)
{
while(input_length--)
{
if(input!='\0')
*output++ = input;
input++;
}
*output = '\0'; /* optional null terminator if this is really a string */
}
void test()
{
char output[100];
char input = "He\0ll\0o";
Copy(input, sizeof(input), output);
}
于 2017-04-28T12:39:34.310 回答
0
不,没有库函数可以做到这一点。你必须自己写。
但是有一个问题:你怎么知道什么时候停止忽视\0
?您的字符串 ( "he\0ll\0o"
) 有三个零字节。你怎么知道停在第三个?
于 2017-04-28T12:19:29.987 回答
0
'\0' in strings 是一种查找字符串结尾的方法(字符串的终止字符)。因此,所有为字符串操作设计的函数都使用 '\0' 来检测字符串的结尾。
现在,如果您想要这样的实现,您需要自己设计。
您将面临的问题是:
1)您将如何确定使用哪个 '\0' 作为终止字符?
因此,对于这样的实现,您需要明确告知用作终止字符的 '\0' 的计数,或者您需要为字符串设置自己的终止字符。
2) 对于您的实现中的任何其他操作,您不能使用预定义的字符串相关函数。
因此,实现您自己的功能来执行这些操作。
于 2017-04-28T12:35:38.073 回答