要求編一個strcpy函數的原型 我大概寫了一下 可是跟預期不一樣 有沒有人願意指點一下
字符串數組先用“”初始化一下,然後fuzhi函數最後加一個'\0',源碼在下面,順便把比較也實現給你好了,
#include "stdio.h"
#include "iostream"
#include
using namespace std;
void fuzhi(const char* src, char* des)
{
int i = 0;
for (; src[i] != '\0'; i++)
{
des[i] = src[i];
}
des[i + 1] = '\0';
}
int bijiao(char* src, char* des)
{
int i = 0;
while (true)
{
if (src[i] == '\0' || des[i] == '\0')
{
if (src[i] == '\0' && des[i] == '\0')
{
return 0;
}
else
{
if (src[i] == '\0' && des[i] != '\0')
return -1;
else
return 1;
}
}
int iState = int(src[i]) == int(des[i]);
if (iState == 1)
{
iState = 0;
}
else
{
iState = int(src[i]) - int(des[i]);
}
if (iState != 0)
{
return iState;
}
i++;
}
}
void main()
{
char src[256] = "I am src string";
char des[255] = "";
fuzhi(src, des);
cout << src << endl;
cout << des << endl;
cout << bijiao(src, des) << endl;
strcpy_s(des, src);
cout << bijiao(src, des) << endl;
system("pause");
}