我需要編寫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