串是字符串的简称。它是一种在数据元素的组成上具有一定约束条件的线性表,即要求组成线性表的所有数据元素都是字符,所以,人们经常这样定义串:串是一个有穷字符序列。如: abcdef
串的长度为6
串中没有任何字符,其串的长度为0,通常称为空串。由空格字符组成的串,一班称为空格串。串中任意连续的字符组成的子序列称为该串的子串。包含子串的串又称为该子串的主串。子串在主串中第一次出现的第一个字符的位置称为子串在主串中的位置。两个串的长度相等,并且各个对应的字符都相同,称两个串相等。串的存储方式也分两种:串的顺序存储结构和串的链式存储结构
我写的是一个堆分配存储的串,代码:
#include<iostream>#include<string>#include<malloc.h>using namespace std;//串的堆存储方式typedef struct { char *str; int length;}HString;void init(HString *s) { s->length = 0; s->str = NULL;}//串初始化void HStringInit(HString *s) { char c; int length; cout << "请输入串的长度:" << endl; cin >> length; s->length = length; s->str = (char*)malloc((length+1)*sizeof(char)); for (int i = 0; i < s->length; i++) { cin >> c; s->str[i] = c; } cout << "初始化成功!" << endl;}//串赋值void HStringAssign(HString *s, HString *t) { if (s->str) free(s->str); int len = t->length; s->length = len; if (!len) { s->str = (char*)malloc(sizeof(char)); s->str[0] = '/0'; } else { s->str = (char*)malloc(sizeof(char)); for (int i = 0; i < t->length; i++) { s->str[i] = t->str[i]; } } cout << "赋值成功!" << endl;}//输出串内容void PRint(HString *s) { if (s->length == 0 && s->str == NULL) { cout << "串的内容为空!" << endl; } else { cout << "串的内容为: "; for (int i = 0; i < s->length; i++) { cout << s->str[i]; } cout << endl; }}//求串长void HStringLength(HString *s ) { cout << "串的长度为: " << s->length << endl;}//判串相等void HStringCompar(HString *s,HString *t) { int flag = 0; if (s->length != t->length) { cout << "不相等!" << endl; } else { if (s->length == 0) { cout << "相等!" << endl; } else { for (int i = 0; i < s->length&&i < t->length; i++) { if (s->str[i] != t->str[i]) { cout << "不相等!" << endl; flag = 1; break; } } if (flag == 0) { cout << "相等!" << endl; } } }}//串连接void HStringConCat(HString *s,HString *t) { HString s1; init(&s1); int i = 0; HStringAssign(&s1, s); s->length += t->length; free(s->str); s->str = (char*)malloc((s->length+1)*sizeof(char)); for (; i < s1.length; i++) { s->str[i] = s1.str[i]; } for (; i < s->length;i++) { s->str[i] = t->str[i - s1.length]; } free(s1.str); free(t->str); t->str = NULL; t->length = 0; cout << "连接完成!" << endl;}//求子串void HStringSub(HString *s,int start,int end) { cout << "子串为:"; for (int i = start - 1; i < end; i++) { cout << s->str[i]; } cout << endl;}//子串定位void HStringIndex(HString *s,HString *t) { int i = 0,j = 0; while (i < s->length &&j < t->length) { if (s->str[i] == t->str[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j == t->length) { cout << "子串的位置为:" << i-t->length + 1 << endl; } else { cout << "没有该子串!" << endl; }}//串置换void HStringReplace(HString *s,HString *t) { HString s1; init(&s1); HStringAssign(&s1, s); HStringAssign(s, t); HStringAssign(t, &s1); free(s1.str); s1.length = 0; s1.str = NULL; cout << "置换成功!" << endl;}//插入子串void HStringInsert(HString *s,int start,HString *t) { HString s1; init(&s1); int i = 0,j; HStringAssign(&s1, s); s->length += t->length; free(s->str); s->str = (char*)malloc((s->length + 1) * sizeof(char)); for (; i < start-1; i++) { s->str[i] = s1.str[i]; } j = i; for (int k = 0; k < t->length; k++, i++) { s->str[i] = t->str[k]; } for (; i < s->length; i++, j++) { s->str[i] = s1.str[j]; } cout << "插入成功!" << endl;}//删除子串void HStringDelet(HString *s,int start,int length) { int i = start,flag,k=length; char c; while (length != 0) { s->str[i - 1] = 0; length--; i++; } for (int m = 0; m < s->length;m++) { if (s->str[m] != 0 && s->str[m - 1] == 0) { flag = m; while (flag) { if (s->str[flag - 1] != 0) { break; } c = s->str[flag - 1]; s->str[flag - 1] = s->str[flag]; s->str[flag] = c; flag--; } } } s->length -=k; cout << "删除成功!" << endl;}void message() { cout << "************************************************************" << endl; cout << "* 1 输出串 2 串赋值 *" << endl; cout << "* 3 判串相等 4 求串长 *" << endl; cout << "* 5 串连接 6 求子串 *" << endl; cout << "* 7 子串定位 8 串置换 *" << endl; cout << "* 9 插入子串 10 删除子串 *" << endl; cout << "* 11 清屏 12 退出 *" << endl; cout << "************************************************************" << endl;}void main() { int temp = 0,length; HString S1, S2; message(); cout << "对串S1进行初始化:" << endl; HStringInit(&S1); cout << "对串S2进行初始化:" << endl; HStringInit(&S2); int i = 0; while (1) { cin >> i; switch (i) { case 1:cout << "请输入你要输出的串 1: S1, 2: S2" << endl; cin >> temp; switch (temp) { case 1:print(&S1); break; case 2:print(&S2); break; default:cout << "输入错误!" << endl; break; } break; case 2: cout << "将S2赋值给S1" << endl; HStringAssign(&S1, &S2); break; case 3:cout << "判断S1和S2是否相等?" << endl; HStringCompar(&S1, &S2); break; case 4:cout << "请输入你要求的串 1: S1, 2: S2" << endl; cin >> temp; switch (temp) { case 1:HStringLength(&S1); break; case 2:HStringLength(&S2); break; default:cout << "输入错误!" << endl; break; } break; case 5:cout << "将串S2连接到S1上!" << endl; HStringConCat(&S1, &S2); break; case 6:cout << "求S1的子串:" << endl; cout << "请输入开始位置和长度:" << endl; cin >> temp >> length; if (temp > S1.length) { cout << "输出位置错误!" << endl; break; } else { if ((length - temp) > S1.length) { cout << "子串长度超出!" << endl; } else { HStringSub(&S1, temp, length); } } break; case 7:cout << "子串S2在S1中的位置" << endl; HStringIndex(&S1,&S2); break; case 8:cout << "将S1和S2置换!" << endl; HStringReplace(&S1,&S2); break; case 9:cout << "将S2插入到S1中" << endl; cout << "请输入插入位置:" << endl; cin >> temp; if (temp >= S1.length) { cout << "插入位置错误!" << endl; break; } else { HStringInsert(&S1,temp,&S2); } break; case 10:cout << "删除S1的子串:" << endl; cout << "请输入开始位置和长度:" << endl; cin >> temp >> length; if (temp > S1.length) { cout << "输出位置错误!" << endl; break; } else { if ((length + temp) > S1.length+1) { cout << "子串长度超出!" << endl; } else { HStringDelet(&S1, temp, length); } }break; case 11:system("cls"); message(); break; case 12:exit(0); break; default:cout << "输入错误!请重新输入:" << endl; break; } }}
新闻热点
疑难解答