strcpy的自我实现
时间: 2024-04-10 22:25:11 浏览: 25
strcpy是一个C语言中常用的字符串复制函数,用于将一个字符串的内容复制到另一个字符串中。下面是一个简单的自我实现示例:
```c
#include <stdio.h>
char* strcpy_custom(char* dest, const char* src) {
if (dest == NULL || src == NULL) {
return NULL;
}
char* temp = dest;
while ((*temp++ = *src++) != '\0') {
;
}
return dest;
}
int main() {
char src[] = "Hello, CSDN!";
char dest[20];
strcpy_custom(dest, src);
printf("Copied string: %s\n", dest);
return 0;
}
```
在自我实现的strcpy_custom函数中,我们首先判断了目标字符串dest和源字符串src是否为空指针,如果为空则返回NULL。然后我们使用一个指针temp指向目标字符串dest的起始位置,通过循环将源字符串src的每个字符逐个复制到目标字符串dest中,直到遇到字符串结束符'\0'为止。最后返回目标字符串dest。
相关问题
请设计实现自己的字符串类(
MyString),要求支持以下操作:
1. 构造函数、拷贝构造函数和析构函数
2. 重载取下标运算符[],实现类似字符指针的访问方式
3. 重载赋值运算符=,实现类似字符指针的赋值方式
4. 重载加号运算符+,实现字符串的连接
5. 重载等于运算符==,实现两个字符串的比较
6. 重载不等于运算符!=,实现两个字符串的比较
7. 重载括号运算符(),实现截取字符串的功能
下面是一个参考实现:
```c++
class MyString {
public:
// 默认构造函数
MyString() {
data_ = new char[1];
*data_ = '\0';
length_ = 0;
}
// 从字符指针构造
MyString(const char* s) {
length_ = strlen(s);
data_ = new char[length_ + 1];
strcpy(data_, s);
}
// 拷贝构造函数
MyString(const MyString& other) {
length_ = other.length_;
data_ = new char[length_ + 1];
strcpy(data_, other.data_);
}
// 析构函数
~MyString() {
delete[] data_;
}
// 取下标运算符[]
char& operator[](int index) {
return data_[index];
}
// 赋值运算符=
MyString& operator=(const MyString& other) {
if (this != &other) { // 防止自我赋值
delete[] data_;
length_ = other.length_;
data_ = new char[length_ + 1];
strcpy(data_, other.data_);
}
return *this;
}
// 加法运算符+
MyString operator+(const MyString& other) {
MyString new_str;
new_str.length_ = length_ + other.length_;
new_str.data_ = new char[new_str.length_ + 1];
strcpy(new_str.data_, data_);
strcat(new_str.data_, other.data_);
return new_str;
}
// 等于运算符==
bool operator==(const MyString& other) const {
return strcmp(data_, other.data_) == 0;
}
// 不等于运算符!=
bool operator!=(const MyString& other) const {
return strcmp(data_, other.data_) != 0;
}
// 括号运算符()
MyString operator()(int start, int end) {
MyString new_str;
if (start < 0 || end >= length_ || start > end) {
return new_str;
}
int new_len = end - start + 1;
new_str.data_ = new char[new_len + 1];
strncpy(new_str.data_, data_ + start, new_len);
new_str.data_[new_len] = '\0';
new_str.length_ = new_len;
return new_str;
}
private:
char* data_;
int length_;
};
```
使用示例:
```c++
MyString s1;
MyString s2("hello");
MyString s3(s2);
MyString s4 = "world";
s1 = s2;
s4 = s2 + s4;
cout << s4 << endl; // 输出 "helloworld"
if (s2 == s3) {
cout << "s2 equals s3" << endl;
} else {
cout << "s2 not equals s3" << endl;
}
cout << s4(2, 4) << endl; // 输出 "llo"
```
自定义类mystring实现运算符=重载实验报告总结
自定义类mystring实现运算符=重载,是一个非常基础也非常重要的C++编程技能。在这个实验中,我们通过对mystring类进行重载运算符=的操作,可以实现将一个mystring对象的值赋给另一个mystring对象。
在本次实验中,我们首先需要对mystring类进行定义,包括构造函数、析构函数、拷贝构造函数等。然后,我们需要重载运算符=,并在其中实现将一个mystring对象的值赋给另一个mystring对象的功能。具体实现过程可以参考以下代码:
```
class mystring {
private:
char* str;
int len;
public:
mystring(const char* s = "");
mystring(const mystring& s);
~mystring();
mystring& operator=(const mystring& s);
};
mystring::mystring(const char* s) {
len = strlen(s);
str = new char[len+1];
strcpy(str, s);
}
mystring::mystring(const mystring& s) {
len = s.len;
str = new char[len+1];
strcpy(str, s.str);
}
mystring::~mystring() {
delete [] str;
}
mystring& mystring::operator=(const mystring& s) {
if (this == &s) {
return *this;
}
delete [] str;
len = s.len;
str = new char[len+1];
strcpy(str, s.str);
return *this;
}
```
在上述代码中,我们首先定义了mystring类,并在其中添加了构造函数、析构函数、拷贝构造函数等。然后,我们重载了运算符=,并在其中实现了将一个mystring对象的值赋给另一个mystring对象的功能。具体实现过程如下:
- 首先判断是否为自我赋值,如果是则直接返回该对象;
- 然后删除原有的str指针,以免内存泄漏;
- 分配新的内存空间,并将新对象s的str指针指向该内存空间;
- 将s的值复制到当前对象中,并返回该对象。
总之,通过本次实验,我们可以深入理解C++中运算符重载的概念和实现方法,为以后更深入的C++编程打下基础。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)