标签:c/c++
编写strcpy函数:已知strcpy函数的原型是char strcpy(char strDest, const char strSrc);编写strlen函数:strlen函数原型是size_t strlen(char *string);
测试:
void main()
{
char str1[ ] = "dsvgfgd";
char str2[ ] = "gvdbh";
strcpy(str1, str2);
printf("%s\n", str1);
int i = strlen(str1);
printf("%d\n", i);
}
String类原型:
#include<stdio.h>
#include<assert.h>
#include<string.h>
class String
{
public:
String(const char str = NULL);
String(const String &another);
~String();
String& operator=(const String &rhs);
private:
char m_data;
};
String::String(const char str)
{
if (str == NULL)
{
m_data = new char[1];
m_data[0] = ‘\0‘;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data, str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data, another.m_data);
}
String& String::operator=(const String &rhs)
{
if (this == &rhs)
return this;
delete [ ]m_data;
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data, rhs.m_data);
return *this;
}
String::~String()
{
delete [ ]m_data;
}
标签:c/c++
原文地址:http://blog.51cto.com/13665347/2103289