【发布时间】:2013-03-02 06:34:56
【问题描述】:
对于我的程序,我需要在不使用标准库或 IO 函数的情况下将 char(char) 添加到 string(char *)。 例如:
char *s = "This is GOO";
char c = 'D';
s = append(s, c);
和 s 会产生字符串“This is GOOD”。 是否有一些适当的方法来操作数组以实现这一目标? 同样,从字符数中生成字符串就足够了。 我很确定我可以使用 malloc,但不是积极的......
char * app(char* s, char c){
char *copy;
int l = strlen_(s);
copy = malloc(l+1);
copy = s;
copy[l] = c;
copy[l+1] = '\0';
return copy;
}
不能使用 strcpy
【问题讨论】:
标签: c arrays string char append