串的基本操作.docx

串的基本操作.docx

ID:58981813

大小:12.16 KB

页数:9页

时间:2020-10-27

串的基本操作.docx_第1页
串的基本操作.docx_第2页
串的基本操作.docx_第3页
串的基本操作.docx_第4页
串的基本操作.docx_第5页
资源描述:

《串的基本操作.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、#include#include#includetypedefstruct{char*str;intlength;}HString;//堆串//初始化voidInitString(HString&S){S.str='';S.length=0;}//堆串赋值voidStrAssign(HString&S,char*chars){inti,len;char*p;p=chars;if(S.str)free(S.str);for(i=0;p[i]!='';i++)le

2、n=i+1;if(!i){S.length=0;S.str='';}else{S.str=(char*)malloc(len*sizeof(char));if(!S.str)exit(-1);for(i=0;i

3、操作intStrCopy(HString&T,HStringS)/*将串S复制给串T*/{inti;T.str=(char*)malloc(S.length*sizeof(char));if(!T.str)return0;for(i=0;i

4、返回0*/{inti;for(i=0;i

5、

6、pos-1>S.length){printf("插入位置错误");return0;}S.str=(char*)realloc

7、(S.str,(S.length+T.length)*sizeof(char));if(!S.str){printf("内存分配失败");return0;}for(i=S.length-1;i>pos-1;i--)S.str[i+T.length]=S.str[i];for(i=0;i

8、*在S中删除pos开始的len个字符*/{inti;char*p;if(pos<0

9、

10、pos+len-1>S.length){printf("删除位置错误,参数len不合法");return0;}p=(char*)malloc(S.length-len);if(!p){printf("内存分配失败");return0;}for(i=0;i

11、-len;free(S.str);S.str=p;return1;}//串的连接操作intStrConcat(HString&T,HStringS)/*将串S连接在T后*/{inti;T.str=(char*)realloc(T.str,(T.length+S.length)*sizeof(char));if(!T.str){printf("内存分配失败");return0;}else{for(i=T.length;i

12、length=T.length+S.length;}return1;}//截取子串操作intSubString(HString&Sub,HStringS,intpos,intlen)/*将从S中第pos个位置截取长度为len的子串赋值给Sub*/{inti;if(Sub.str)free(

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。