所以我的任务是:
使用#include<cstring>的strncpy和strncat函数,实现一个函数
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)将字符串a和b连接到缓冲区result 。 一定不要超过结果。 它可以保存result_maxlength字符,不包括\0终止符。 (也就是说,缓冲区有buffer_maxlength + 1个字节可用。)确保提供'\ 0'终止符。
我的解决方案(到目前为止)在下面,但我不知道我做错了什么。 我实际运行程序时不仅会遇到运行时检查失败2错误,而且我不确定应该在哪里添加\0终止符,或者即使我应该使用strncat而不是strncpy 。 希望有人能带领我朝着正确的方向前进。 是的,这是hw。 这就是为什么我说只是引导我走向正确的方向,这样我就可以试着弄清楚:p
#include <iostream> #include <cstring> using namespace std; void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength); int main() { char a[] = "Woozle"; char b[] = "Heffalump"; char c[5]; char d[10]; char e[20]; concat(a, b, c, 5); concat(a, b, d, 10); concat(a, b, e, 20); cout << c << "\n"; cout << d << "\n"; cout << e << "\n"; return 0; } void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength) { strncat(result, a, result_maxlength); strncat(result, b, result_maxlength); }So my assignment is:
Using the strncpy and strncat functions in #include<cstring>, implement a function
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)that concatenates the strings a and b to the buffer result. Be sure not to overrun the result. It can hold result_maxlength characters, not counting the \0 terminator. (That is, the buffer has buffer_maxlength + 1 bytes available.) Be sure to provide a ‘\0’ terminator.
My solution (thus far) is below but I don't know what I'm doing wrong. Not only do I get a run-time check failure 2 error when I actually run the program, but I'm unsure where I should be adding the \0 terminator or even if I should be using strncat rather than strncpy. Hopefully someone can lead me in the right direction. And yes this is hw. That's why I said just lead me in the right direction so that I can try to figure it out :p
#include <iostream> #include <cstring> using namespace std; void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength); int main() { char a[] = "Woozle"; char b[] = "Heffalump"; char c[5]; char d[10]; char e[20]; concat(a, b, c, 5); concat(a, b, d, 10); concat(a, b, e, 20); cout << c << "\n"; cout << d << "\n"; cout << e << "\n"; return 0; } void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength) { strncat(result, a, result_maxlength); strncat(result, b, result_maxlength); }最满意答案
从a到结果的strncpy(无论是更小的,还是a或result_maxlength的长度)strncat从b到剩余的结果(无论是较小的,b的长度还是a的result_maxlength-长度)
在每次返回之前,只需将\ 0置于最后位置result[result_maxlength-1] ='\0';
它实际上没有指定如果结果太短会怎么做,你是否应该添加尾随0。 我想你最好终止那个字符串。
提示:剩下的结果是result+strlen(a)
strncpy from a to result (whatever is smaller, lenght of a or result_maxlength)strncat from b to remaining of result (whatever is smaller, lenght of b or result_maxlength- lenght of a)
before every return just put a \0 at last position result[result_maxlength-1] ='\0';
It's actually not specified WHAT to do if result is too short, should you add trailing 0 or not. I guess you'd better terminate that string.
tip : remaining of result is result+strlen(a)
更多推荐
发布评论