您的位置:首页 > 理论基础 > 数据结构算法

严蔚敏C语言版数据结构之串的全部操作

2020-04-02 08:01 796 查看
#include<stdio.h>#include<stdlib.h>#define ok 1#define error 0#define overflow -1#define maxstrlen 255typedef unsigned char sstring[maxstrlen + 1];//s[0]用来存放长度typedef int status;typedef char elemtype;status strassign(sstring &s)//生成字符串{int i = 1;char ch;//printf("请输入字符串:");scanf_s("%c", &ch);while (ch != '\n'){s[i] = ch;i++;scanf_s("%c", &ch);}s[0] = i - 1;return ok;}status strcopy(sstring &t, sstring s)//有串S复制得串T{if (!s)return ok;//s是空串则无法复制int i;for (i = 1; i <= s[0]; i++)t[i] = s[i];t[0] = s[0];return ok;}status strprint(sstring s)//遍历字符串{int i;for (i = 1; i <= s[0]; i++)//因为s[0]用来存放长度,所以从1开始printf("%c", s[i]);printf("\n");return ok;}status listempty(sstring s)//判断s是否为空串{if (s[0] == 0)return ok;elsereturn error;}void strlength(sstring s)//输出字符串的长度{printf("\n此字符串的长度为:%d\n", s[0]);}int strcompare(sstring s, sstring t)//s与t比较大小{if (!t || !s)return error;int i;for (i = 1; i <= s[0] && i <= t[0];i++)if (s[i] != t[i])//当这两个字符串中有字符不相等时,则比较成功return s[i] - t[i];return s[0] - t[0];//说明其中一个字符串为另一个字符串中的字串或两字符串相等,则长度相减即可}status concat(sstring &t, sstring s1, sstring s2)//将串s1和s2连接起来,用t返回{int i;if (s1[0] + s2[0] <= maxstrlen + 1)//未截断{for (i = 1; i <= s1[0]; i++)t[i] = s1[i];for (i = 1; i <= s2[0]; i++)t[i+s1[0]] = s2[i];//此时串s1已经复制好了,所以t从s[0]+1开始t[0] = s1[0] + s2[0];//t的长度为s1+s2return ok;}else if (s1[0]<maxstrlen){for (i = 1; i <= s1[0]; i++)t[i] = s1[i];for (i = 1; i <= maxstrlen - s1[0]; i++)t[s1[0] + i] = s2[i];t[0] = maxstrlen;return error;}else{for (i = 1; i <= maxstrlen; i++)t[i] = s1[i];t[0] = maxstrlen;return error;}}status subtring(sstring &sub,sstring s,int pos,int len)//用sub返回串s 的第pos个长度为len的字串{if (pos<1 || pos>s[0] || len<0 || len>s[0] - pos + 1)return error;int i;for (i = pos; i <= pos + len - 1; i++)sub[i - pos+1] = s[i];//因为i从pos开始,且sub[0]是用来存放长度sub[0] = len;return ok;}int index(sstring s, sstring t, int pos)//求串t在s中第一次出现的位置{if (!t || pos<1 || pos>s[0])return error;int i = pos;//i-主串S中当前位置下标,若pos不为1,从pos位置开始匹配int j = 1;// //j用于子串T中当前位置的下标值while (i <= s[0] && j <= t[0])//如果j>t[0]说明匹配成功,跳出循环{if (s[i] == t[j])//两字母相同时,继续循环,向后移动{i++;j++;}else//俩字母不同时,i退回到上次匹配首位的下一位,j重置为1{i = i - j + 2;j = 1;}}if (j >t[0])return i - t[0];//或者是返回i-j+1也可以elsereturn error;//i>s[0],则匹配失败}status clearstr(sstring &s)//置空串{s[0] = 0;return ok;}status strinser(sstring &s, int pos, sstring t)//在串s的第pos个字符之前插入串t{if (!s || !t || pos<1 || pos>s[0] + 1)return error;int i,j;if (s[0] + t[0] < maxstrlen){s[0] += t[0];for (i = pos+t[0]; i <= s[0]; i++)s[i ] = s[i-t[0]];//pos的后半部分往后移for (i = pos,j=1; j<=t[0]; i++, j++)s[i] = t[j];//在pos的位置开始插入t,j控制t的小标return ok;}else{for (i = pos+t[0]; i <= maxstrlen; i++)s[i] = s[i - t[0]];for (i = pos; i<pos + t[0] && i <= maxstrlen; i++)s[i] = t[i - pos + 1];s[0] = maxstrlen;return ok;}}status strdelete(sstring &s, int pos, int len)//从串s中删除第pos个字符起长度为len的字串{if (!s || pos<1 || pos>s[0] - len + 1)return error;int i;for (i = pos; i <= pos + len - 1; i++)s[i] = s[i + len];//pos+len后的位置往前移s[0] -= len;return ok;}status destorystring(sstring &s)//串s被销毁{free(s);return ok;}int main(){sstring s,t,s1,s2,sub;int i=3,m;int pos = 2;printf("输入字符串s:");strassign(s);printf("s:");strprint(s);strlength(s);strcopy(t, s);printf("将串s复制到串t\n");printf("t:");strprint(t);printf("输入新的字符串t:");strassign(t);printf("t:");strprint(t);m = strcompare(s, t);if (m > 0)printf("串s大于串t\n");else if (m == 0)printf("串s和串t相等\n");elseprintf("串s小于串t\n");printf("将串s和串t链接后的字符串 s1:");concat(s1, s, t);strprint(s1);printf("当pos=5,len=4时s的字串为:");subtring(sub, s, 5, 4);strprint(sub);i = index(s, t,pos);if (i == 0)printf("串t在串s中不存在\n");elseprintf("t在从%d个字符之后在s中出现的的第一个位置是%d\n" ,pos,i);printf("在s的第五个位置插入字符串t后,s:");strinser(s, 5, t);strprint(s);printf("在s的第五个位置起删除长度为4的字符串后,s:");strdelete(s, 5, 4);strprint(s);//destorystring(s);system("pause");return 0;}```c在这里插入代码片
  • 点赞
  • 收藏
  • 分享
  • 文章举报
空指针*p=NULL发布了7 篇原创文章 · 获赞 1 · 访问量 352私信关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: