#include <iostream>
using namespace std;
char *strcpy(char *strDest, const char *strSrc)
{
if(strDest == NULL || strSrc == NULL)
return NULL;
if(strDest == strSrc)
return strDest;
char *tempptr = strDest;
while((*strDest++ = *strSrc++) != '\0')
;
return tempptr;
}
int strLen(const char* strSrc)
{
int len = 0;
while(*strSrc++ != '\0')
{
len++;
}
return len;
}
int main()
{
char strSrc[] = "Hello World!";
char strDest[20];
int length = 0;
length = strLen(strcpy(strDest,strSrc));
cout<< strDest<<endl;
cout<<length<<endl;
system("pause");
return 0;
}
strcpy函数返回char *类型的原因是为了能使用链式表达式。首先调用strcpy使得strDest指针复制strSrc的内存数据,然后调用getStrLen函数获取strDest字符串的长度。这样不仅调用方便,而且程序结构简洁明了。