c++实现数据结构串的基本操作全部代码
时间: 2023-05-30 17:07:34 浏览: 80
以下是 C 语言实现数据结构串的基本操作的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100 // 串的最大长度
typedef struct {
char data[MAXSIZE];
int length;
} SqString; // 串的结构体
// 初始化串
void InitString(SqString *s) {
s->length = 0;
}
// 串赋值
void StrAssign(SqString *s, char *str) {
int len = strlen(str);
if (len > MAXSIZE) {
printf("Error: string is too long.\n");
return;
}
for (int i = 0; i < len; i++) {
s->data[i] = str[i];
}
s->length = len;
}
// 串复制
void StrCopy(SqString *s, SqString t) {
if (t.length > MAXSIZE) {
printf("Error: string is too long.\n");
return;
}
for (int i = 0; i < t.length; i++) {
s->data[i] = t.data[i];
}
s->length = t.length;
}
// 串比较
int StrCompare(SqString s, SqString t) {
int len = s.length < t.length ? s.length : t.length;
for (int i = 0; i < len; i++) {
if (s.data[i] != t.data[i]) {
return s.data[i] - t.data[i];
}
}
return s.length - t.length;
}
// 串连接
void Concat(SqString *s, SqString t) {
if (s->length + t.length > MAXSIZE) {
printf("Error: string is too long.\n");
return;
}
for (int i = s->length, j = 0; j < t.length; i++, j++) {
s->data[i] = t.data[j];
}
s->length += t.length;
}
// 串求子串
void SubString(SqString *sub, SqString s, int pos, int len) {
if (pos < 1 || pos > s.length || len < 0 || len > s.length - pos + 1) {
printf("Error: invalid substring.\n");
return;
}
for (int i = pos - 1, j = 0; j < len; i++, j++) {
sub->data[j] = s.data[i];
}
sub->length = len;
}
// 串插入
void StrInsert(SqString *s, int pos, SqString t) {
if (pos < 1 || pos > s->length + 1 || s->length + t.length > MAXSIZE) {
printf("Error: invalid string insert.\n");
return;
}
for (int i = s->length - 1; i >= pos - 1; i--) {
s->data[i + t.length] = s->data[i];
}
for (int i = pos - 1, j = 0; j < t.length; i++, j++) {
s->data[i] = t.data[j];
}
s->length += t.length;
}
// 串删除
void StrDelete(SqString *s, int pos, int len) {
if (pos < 1 || pos > s->length || len < 0 || len > s->length - pos + 1) {
printf("Error: invalid string delete.\n");
return;
}
for (int i = pos - 1 + len; i < s->length; i++) {
s->data[i - len] = s->data[i];
}
s->length -= len;
}
// 输出串
void PrintString(SqString s) {
for (int i = 0; i < s.length; i++) {
printf("%c", s.data[i]);
}
printf("\n");
}
int main() {
SqString s, t, sub;
char str[MAXSIZE];
int len, pos;
// 初始化串
InitString(&s);
InitString(&t);
InitString(&sub);
// 串赋值
printf("Enter a string: ");
scanf("%s", str);
StrAssign(&s, str);
printf("The string is: ");
PrintString(s);
// 串复制
StrCopy(&t, s);
printf("The copy of the string is: ");
PrintString(t);
// 串比较
printf("Enter another string to compare: ");
scanf("%s", str);
StrAssign(&t, str);
if (StrCompare(s, t) == 0) {
printf("The two strings are equal.\n");
} else {
printf("The two strings are not equal.\n");
}
// 串连接
printf("Enter a string to concatenate: ");
scanf("%s", str);
StrAssign(&t, str);
Concat(&s, t);
printf("The concatenated string is: ");
PrintString(s);
// 串求子串
printf("Enter the start position and length of the substring: ");
scanf("%d%d", &pos, &len);
SubString(&sub, s, pos, len);
printf("The substring is: ");
PrintString(sub);
// 串插入
printf("Enter the position and string to insert: ");
scanf("%d%s", &pos, str);
StrAssign(&t, str);
StrInsert(&s, pos, t);
printf("The inserted string is: ");
PrintString(s);
// 串删除
printf("Enter the position and length of the substring to delete: ");
scanf("%d%d", &pos, &len);
StrDelete(&s, pos, len);
printf("The deleted string is: ");
PrintString(s);
return 0;
}
```
相关推荐
![](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)