strncat & strncpy help c++

偶尔善良 提交于 2019-12-24 03:57:11

问题


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);
}

回答1:


  1. strncpy from a to result (whatever is smaller, lenght of a or result_maxlength)
  2. strncat from b to remaining of result (whatever is smaller, lenght of b or result_maxlength- lenght of a)

  3. 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)




回答2:


At the very least your result is uninitialized before the first strncat in concat.

EDIT: And yes, as Michael Burr points out your result size is supposed to be changing as you progress and calculated from the very start. Actually, it is misleading name you picked, because it is the max size of source, not destination.




回答3:


The last argument to strncat() represents the remaining space available in the buffer - not the full size of the buffer.

Also note that that argument includes the spot that the terminating null character will need, so you'll need to account for that since the spec for concat() is otherwise.

Finally, according to the concat() spec, the result placed in the buffer should not be concatenated to the existing contents of the buffer (those contents should be replaced). Also, make sure you test that your function properly handles a zero length result_maxlength argument being passed in.



来源:https://stackoverflow.com/questions/12217528/strncat-strncpy-help-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!