建立一个串,实现串的基本操作。主要有: 1)判串长: 返回串s的元素个数。 2)判空串: 若串s为空串,则返回true 3)串赋值:现有串s,空串 t,实现将s赋值给t的操作 4)串连接:现有串s和 t,实现将t链接到串s之后的操作。 5)串比较: 依次比较2个串s和t,若长度相同且字符相同,则串相同,返回0,若s>t,则返回值>0, 若s<t,则返回值<0. 6)串替换:在主串s中用子串v替换所有的子串t 7)串插入:在主串s中的给定位置pos插入子串t 8)取子串: 将串s中从起始位置pos开始长度len的字符串放入另一个串t 9)串删除: 删除串s中的子串t。 10)求子串位置:从主串s中取出从第i个位置开始、长度和串t相同的子串,和t进行比较,若相等,则t在s中的位置值为i。 二、编码实现
时间: 2023-05-20 07:05:35 浏览: 65
1)判串长:
```
int strLength(char* s){
int len = 0;
while(s[len] != '\0'){
len++;
}
return len;
}
```
2)判空串:
```
bool isEmpty(char* s){
if(s[0] == '\0'){
return true;
}
return false;
}
```
3)串赋值:
```
void strCopy(char* s, char* t){
int i = 0;
while(s[i] != '\0'){
t[i] = s[i];
i++;
}
t[i] = '\0';
}
```
4)串连接:
```
void strConcat(char* s, char* t){
int i = 0, j = 0;
while(s[i] != '\0'){
i++;
}
while(t[j] != '\0'){
s[i] = t[j];
i++;
j++;
}
s[i] = '\0';
}
```
5)串比较:
```
int strCompare(char* s, char* t){
int i = 0;
while(s[i] != '\0' && t[i] != '\0'){
if(s[i] > t[i]){
return 1;
}
else if(s[i] < t[i]){
return -1;
}
i++;
}
if(s[i] == '\0' && t[i] == '\0'){
return 0;
}
else if(s[i] == '\0'){
return -1;
}
else{
return 1;
}
}
```
6)串替换:
```
void strReplace(char* s, char* t, char* v){
int slen = strLength(s);
int tlen = strLength(t);
int vlen = strLength(v);
int i = 0, j = 0, k = 0;
while(i < slen){
if(s[i] == t[j]){
j++;
if(j == tlen){
for(int l = 0; l < vlen; l++){
s[k] = v[l];
k++;
}
j = 0;
}
}
else{
s[k] = s[i];
k++;
j = 0;
}
i++;
}
s[k] = '\0';
}
```
7)串插入:
```
void strInsert(char* s, int pos, char* t){
int slen = strLength(s);
int tlen = strLength(t);
for(int i = slen; i >= pos; i--){
s[i+tlen] = s[i];
}
for(int i = 0; i < tlen; i++){
s[pos+i] = t[i];
}
}
```
8)取子串:
```
void subString(char* s, int pos, int len, char* t){
int slen = strLength(s);
int i = pos, j = 0;
while(i < slen && j < len){
t[j] = s[i];
i++;
j++;
}
t[j] = '\0';
}
```
9)串删除:
```
void strDelete(char* s, char* t){
int slen = strLength(s);
int tlen = strLength(t);
int i = 0, j = 0, k = 0;
while(i < slen){
if(s[i] == t[j]){
j++;
if(j == tlen){
j = 0;
}
}
else{
s[k] = s[i];
k++;
j = 0;
}
i++;
}
s[k] = '\0';
}
```
10)求子串位置:
```
int subStringPos(char* s, int i, char* t){
int slen = strLength(s);
int tlen = strLength(t);
int j = 0;
while(i < slen){
if(s[i] == t[j]){
j++;
if(j == tlen){
return i-tlen+1;
}
}
else{
j = 0;
}
i++;
}
return -1;
}
```
以上是基本操作的实现,可以根据需要进行调用。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)