#include <stdio.h>
char* strcpy(char * strDest, const char* strSrc)
{
int i = 0;
if((strDest == NULL) || (strSrc == NULL)) //判断两个空间的有效性
{
return NULL;
}
//char* strDestCopy = strDest;
while((strDest[i++] = *strSrc++) != '\0'); //把strSrc字符串的内容复制到strDest中
return strDest;
}
int getStrLen(const char *strSrc) //获取strSrc字符串的长度
{
int len = 0;
while(*strSrc++ != '\0') //遇到‘\0’停止
{
len++;
}
return len;
}
int main()
{
char strSrc[] = "hello world!";
char strDest[20];
int len = 0;
len = getStrLen(strcpy(strDest, strSrc));
printf("strDest: %s\n",strDest);
printf("Length of strDest : %d\n", len);
return 0;
}
//#include <iostream>
//using namespace std;
//
//int main()
//{
// int i = 0;
// char a[100] = "hello";
// char b[] = "hi";
// while (b[i])
// {
// a[i] = b[i];
// i++;
// }
// //printf("%s\n", strcpy(a, b));
// printf("%s\n", a);
// return 0;
//}
不使用库函数实现strcpy,strlen函数
最新推荐文章于 2023-03-10 21:52:26 发布