c++编程,用动态数组来存放字符串的每个字符,实现以下函数:求长度Strlen,串连接StrConcat,求子串Substr,按字典序比较两个串Strcmp,在串中指定位置插入StrIns,在串中指定位置删除StrDel,把串中的对应子串换成另一字符串StrRep
时间: 2024-11-06 12:24:10 浏览: 35
在C++中,使用动态数组存储字符串并实现上述功能,你需要定义一个包含字符的动态数组结构,并为每个函数编写相应的实现。这里是一个简单的示例:
```cpp
#include <iostream>
#include <cstring>
class String {
private:
char* data;
int length;
public:
// 构造函数
String(const char* str = "") : length(strlen(str) + 1) {
if (str != nullptr) {
data = new char[length];
std::copy(str, str + strlen(str), data);
data[length - 1] = '\0'; // 结尾添加空字符
} else {
data = new char[1]; // 空字符串
data[0] = '\0';
}
}
~String() { delete[] data; }
// 求长度
int Strlen() const { return length; }
// 串连接
String StrConcat(const String& other) const {
String result(*this);
result.length += other.length;
result.data = new char[result.length];
std::copy(result.data, result.data + result.length - 1, result.data + result.length - other.length);
std::copy(other.data, other.data + other.length, result.data);
result.data[result.length - 1] = '\0';
return result;
}
// 求子串
String Substr(int start, int end = -1) const {
if (end == -1)
end = length - 1;
String substr;
substr.length = end - start + 1;
substr.data = new char[substr.length];
std::copy(data + start, data + end + 1, substr.data);
substr.data[substr.length - 1] = '\0';
return substr;
}
// 字典序比较
bool Strcmp(const String& other) const {
for (int i = 0; i < std::min(length, other.length); ++i) {
if (data[i] != other.data[i])
return data[i] < other.data[i];
}
return length <= other.length;
}
// 在串中指定位置插入
String StrIns(int pos, const String& insertStr) {
String temp(*this);
int newLength = length + insertStr.length;
temp.length = newLength;
temp.data = new char[newLength];
std::copy(temp.data, temp.data + pos, temp.data);
std::copy(insertStr.data, insertStr.data + insertStr.length, temp.data + pos);
std::copy(temp.data + pos + insertStr.length, temp.data + length, temp.data + pos + insertStr.length);
return temp;
}
// 在串中指定位置删除
String StrDel(int pos, int len = 1) {
String temp(*this);
int newLength = length - len;
temp.length = newLength;
temp.data = new char[newLength];
if (pos > newLength) pos = newLength;
std::copy(temp.data + pos, temp.data + length, temp.data);
return temp;
}
// 串替换
String StrRep(const String& search, const String& replace) {
String temp(*this);
for (int i = 0; i < temp.length; ++i) {
if (temp.data[i] == search.data[0]) {
temp.data[i] = replace.data[0];
size_t found = std::search(temp.data + i + 1, temp.data + temp.length,
search.data + 1, search.data + search.length);
if (found != temp.data + temp.length) {
temp.Length -= found - temp.data + i + 1;
temp.data += found - temp.data + i + 1;
temp.Length++;
}
}
}
return temp;
}
};
// 示例用法
int main() {
String s("Hello");
std::cout << "Length: " << s.Strlen() << std::endl;
String t("World");
std::cout << "Concat: " << s.StrConcat(t).data << std::endl;
std::cout << "Substring: " << s.Substring(6).data << std::endl;
String u("Hello");
std::cout << "Compare: " << (u.Strcmp(s) ? "Less" : "Equal") << std::endl;
s = s.StrIns(5, "There");
std::cout << "Insertion: " << s.data << std::endl;
s = s.StrDel(7, 5);
std::cout << "Deletion: " << s.data << std::endl;
s = s.StrRep("o", "*");
std::cout << "Replacement: " << s.data << std::endl;
return 0;
}
```
注意,这个示例中的所有操作假设了字符串的内存管理是安全的,实际项目中应使用智能指针等手段处理内存分配与释放。
阅读全文