//实现strlen和strcpy函数
#include<iostream>
#include<cstring>
#include<assert.h>
using namespace std;
char* m_strcpy(char*strDest, const char*strSrc){
assert(strDest != NULL && strSrc != NULL);
char* address = strDest;
while (*strSrc != '\0'){
*strDest = *strSrc;
strDest++;
strSrc++;
}
*strDest = '\0';
return address;//注意不要返回strDest,因为此时strDest已经移动到字符串末尾
}
int m_strlen(const char* str){
assert(str != NULL);
int len = 0;
while (*str != '\0'){
len++;
str++;
}
return len;
}
int main(){
char* str1 = "hello";
char* str2 = new char[6];
m_strcpy(str2, str1);
cout << m_strlen(str1) << endl;
cout << m_strlen(str2) << endl;
}
实现strcpy和strlen函数
最新推荐文章于 2021-05-18 10:11:34 发布