#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int StringLength(char *s)
{
int i = 0;
while (*(s + i) != '\0')
{
i++;
}
return i;
}
char* StringNCopy(char* dest, char* src, int numChar)
{
int lengthSrc = StringLength(src);
if (numChar > lengthSrc)
numChar = lengthSrc;
dest = new char[numChar];
int i = 0;
while (i<numChar)
{
dest[i] = src[i];
i++;
}
dest[numChar] = '\0';
return dest;
}
char* StringCopy(char* dest, char* src)
{
int LengthDest = StringLength(dest);
int LengthSrc = StringLength(src);
return 0;
}
void OutputString(char* s, int n)
{
for (int i = 0; i < n; i++)
{
cout << *(s + i);
}
}
void main()
{
char* src=new char[];
char* dest = new char[];
int numChar;
cout << "Enter source string :";
gets(src);
cout << "Enter number of character :";
cin >> numChar;
dest = StringNCopy(dest,src,numChar);
OutputString(dest, numChar);
delete[] dest;
delete[] src;
system("pause");
}
Em viết hàm strncopy nhưng khi lúc test trong hàm main thì có lỗi vùng nhớ
Viết thế này sai 100% dù có đúng thì chắc đc vài char là cùng.
1 Like
vậy làm sao đây anh