我需要编写void strncat,它将源的第一个数字字符追加到目标,再加上一个终止空字符。如果源文件中C字符串的长度小于num,则只复制直到终止空字符的内容。我究竟做错了什么?strncat函数C++不工作
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int MAXDESTINATION = 40;
const int MAXSOURCE = 150;
int main() {
char dest[MAXDESTINATION + 1] = "It don't mean a thing";
char src[MAXSOURCE + 1] = " if it don't got the Go-Go swing!";
int a = strlen(dest);
int b = strlen(src);
strncat(dest, src, MAXDESTINATION - a) ;}
void strncat(char destination[], const char source[], int num) {
if (int strlen(source) < num) {
int begin = 0;
bool less = false;
for (int i = 0; i <num; i++) {
if (destination[i] == '\0') {
begin = i;
less = true;
}
if (less == true) {
destination[begin] = source[i];
}
}
}
我假设你谈论你的编译器错误?因为你展示的代码不会构建。如果发布关于构建错误的问题,请始终在问题主体中包含完整和未编辑的构建输出,并将错误所在的代码行标记为(例如注释)。 –
@JoachimPileborg“如果源文件中C字符串的长度小于num,则只复制到达终止空字符的内容”听起来不像编译器错误 – xaxxon
我不知道'if(int strlen (源)
xaxxon