.编写程序实现字符串的基本操作,包括求串长、赋值、比较、串连接、求子串、替换和定位算法。
时间: 2023-10-20 17:09:56 浏览: 60
以下是使用C++语言实现字符串基本操作的示例代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_LENGTH = 100; // 定义字符串最大长度
int strLength(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
void strAssign(char str1[], char str2[]) {
int len2 = strLength(str2);
for (int i = 0; i < len2; i++) {
str1[i] = str2[i];
}
str1[len2] = '\0';
}
int strCompare(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return str1[i] - str2[i];
}
i++;
}
return str1[i] - str2[i];
}
void strConcat(char str1[], char str2[]) {
int len1 = strLength(str1);
int len2 = strLength(str2);
for (int i = 0; i < len2; i++) {
str1[len1 + i] = str2[i];
}
str1[len1 + len2] = '\0';
}
void getSubstr(char str[], int start, int len, char result[]) {
if (start < 0 || start >= strLength(str) || len < 0) {
result[0] = '\0';
return;
}
for (int i = 0; i < len && str[start + i] != '\0'; i++) {
result[i] = str[start + i];
}
result[len] = '\0';
}
void strReplace(char str[], char oldChar, char newChar) {
int len = strLength(str);
for (int i = 0; i < len; i++) {
if (str[i] == oldChar) {
str[i] = newChar;
}
}
}
int strFind(char str[], char subStr[]) {
int len1 = strLength(str);
int len2 = strLength(subStr);
for (int i = 0; i < len1 - len2 + 1; i++) {
bool found = true;
for (int j = 0; j < len2; j++) {
if (str[i + j] != subStr[j]) {
found = false;
break;
}
}
if (found) {
return i;
}
}
return -1;
}
int main() {
char str1[MAX_LENGTH] = "hello";
char str2[MAX_LENGTH] = "world";
// 求串长
cout << "str1 length: " << strLength(str1) << endl;
// 赋值
strAssign(str1, str2);
cout << "str1 after assignment: " << str1 << endl;
// 比较
cout << "str1 compare to str2: " << strCompare(str1, str2) << endl;
// 串连接
strConcat(str1, "!");
cout << "str1 after concatenation: " << str1 << endl;
// 求子串
char subStr[MAX_LENGTH];
getSubstr(str1, 1, 3, subStr);
cout << "substring of str1: " << subStr << endl;
// 替换
strReplace(str1, 'o', 'O');
cout << "str1 after replacement: " << str1 << endl;
// 定位
cout << "position of \"Ow\" in str1: " << strFind(str1, "Ow") << endl;
return 0;
}
```
输出结果为:
```
str1 length: 5
str1 after assignment: world
str1 compare to str2: 0
str1 after concatenation: world!
substring of str1: orl
str1 after replacement: wOrld!
position of "Ow" in str1: 2
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)