串的基本操作 - 数据结构 - 机器学习
1228 人阅读 | 时间:2021年01月15日 01:07
数据结构 - 机器学习
深度学习

当前位置:首页 » 数据结构精品文章 » 正文
串的基本操作
1290 人参与 2018年09月03日 22:57 分类 : 数据结构精品文章 评论
串的基本操作
前记:这一章课件里主要讲了串的属性和一些常用的操作。课件里面是通过伪代码的方式来进行描述,这样有利于同学们的理解,以及能够适用于各种编程语言。下面就针对C++语言对这些基本操作做一个具体的实现,大家在运用中可以参考。
mystring.h:
#ifndef MYSTRING_H #define MYSTRING_H #include <stdio.h> #include <stdlib.h> #include <windows.h> #define MAXSIZE 1000 #define SIZEINCREASE 10 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define FORMAT "%3c" #define TURNLINE printf("\n\n") typedef long Status; typedef char ElemType; typedef struct { ElemType *ch; int length; //字符串的长度,不包含空字符 }String; void test(); void strPrint(String *); //打印字符串 Status strAssign(String *,char *); //复制字符串 Status strCopy(String *sourceString,String *destinationString); //复制字符串 int strCompare(String *s1,String *s2); //比较两个字符串 Status strClear(String *s); //清理字符串 Status strContact(String *s3,String *s1,String *s2); //连接两个字符串 Status subString(String *sub,String *s,int pos,int len); //返回某个字符串在第i个位置的子串 void test() { printf("step here..."); TURNLINE; } void strPrint(String *s) { int index; for(index=0;s->ch[index];++index) printf(FORMAT,s->ch[index]); TURNLINE; } Status strAssign(String *s,char *chars) { int i=0,index=0; char *c=NULL; c=chars; for(;*c!='\0';++i,++c); if(!i) { s->ch=NULL; s->length=0;} Else { s->ch=(ElemType *)malloc((i+1)*sizeof(ElemType)); if(!s->ch) exit(OVERFLOW); for(;index<=i;++index) { s->ch[index]=chars[index];} s->ch[i+1]='\0'; s->length=i;} return OK; } Status strCopy(String *sourceString,String *destinationString) { int index=0; if(sourceString->length ==0) { printf("源串为空串"); TURNLINE; } destinationString->ch=(ElemType *)malloc((sourceString->length+1)*sizeof(ElemType)); if(!destinationString) exit(OVERFLOW); destinationString->length =sourceString->length ; for(;index<=sourceString->length ;++index) destinationString->ch[index]=sourceString->ch[index]; destinationString->ch[sourceString->length +1]='\0'; return OK; } int strCompare(String *s1,String *s2) { int i=0; for(;i<=s1->length &&i<=s2->length ;++i) { if(s1->ch[i]!=s2->ch[i]) return s1->ch[i]-s2->ch[i]; } return s1->length -s2->length ; } Status strClear(String *s) { free(s->ch); s->ch=NULL; s->length =0; return OK; } Status strContact(String *s3,String *s1,String *s2) { int index=0,j=s1->length ; s3->ch=(ElemType *)malloc((s1->length+s2->length+1)*sizeof(ElemType));if(!s3->ch) exit(OVERFLOW); for(;s1->ch[index];++index) { s3->ch[index]=s1->ch[index]; } for(index=0;s2->ch[index] ;++index,++j) { s3->ch[j]=s2->ch[index]; } s3->ch[s1->length+s2->length+1]='\0'; s3->length =s1->length+s2->length; return OK; } Status subString(String *sub,String *s,int pos,int len) { int index,i; if(pos<1||pos>s->length||len<0) exit(OVERFLOW); sub->ch=(ElemType *)malloc((len+1)*sizeof(ElemType)); if(!sub) exit(OVERFLOW); for(index=pos-1,i=0;i<len;++index,++i) sub->ch[i]=s->ch[index]; sub->ch[i+1]='\0'; sub->length=i; return OK; } #endif 调用的main函数: #include "mystring.h" #include "fun.h" int main() { String str1,str2,str3;int index; char *chars="acabaabaabcacaabc\0"; char *chars1="abc\0"; strAssign(&str1,chars); strAssign(&str2,chars1); strPrint(&str1); strPrint(&str2); subString(&str3,&str1,5,100); strPrint(&str3); index=Index(&str2,&str1,9); printf("%d\n",index); return 0; }
来源:我是码农,转载请保留出处和链接!
本文链接:http://www.54manong.com/?id=205
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646208",
container: s
});
})();
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646147",
container: s
});
})();
微信号:qq444848023 QQ号:444848023
加入【我是码农】QQ群:864689844(加群验证:我是码农)
- 怎样学好数据结构2018-08-18 08:52
- 数据结构相关基本概念2018-09-03 22:30
- 数据结构知识点总结(一)2018-09-10 21:33
- 学习数据结构的意义和作用2018-09-03 22:48
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646186",
container: s
});
})();
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646175",
container: s
});
})();
搜索
网站分类
- 数据结构
- 数据结构视频教程
- 数据结构练习题
- 数据结构试卷
- 数据结构习题解析
- 数据结构电子书
- 数据结构精品文章
- 区块链
- 区块链精品文章
- 区块链电子书
- 大数据
- 大数据精品文章
- 大数据电子书
- 机器学习
- 机器学习精品文章
- 机器学习电子书
- 面试笔试
- 物联网/云计算
标签列表
- 数据结构 (39)
- 数据结构电子书 (20)
- 数据结构习题解析 (8)
- 数据结构试卷 (10)
- 区块链是什么 (261)
- 数据结构视频教程 (31)
- 大数据技术与应用 (12)
- 百面机器学习 (14)
- 机器学电子书 (29)
- 大数据电子书 (37)
- 程序员面试 (10)
- RFID (21)
最近发表
- 找出数组中有3个出现一次的数字
- 《百面机器学习》电子书下载
- 区块链精品电子书《深度探索区块链:Hyperledger技术与应用_区块链技术丛书》张增骏
- 区块链精品电子书《比特币:一个虚幻而真实的金融世界》
- 区块链精品电子书《图说区块链》-徐明星 & 田颖 & 李霁月
- 区块链精品电子书《是非区块链:技术、投机与泡沫》-英国《金融时报》
- 区块链精品电子书《商业区块链:开启加密经济新时代》-威廉·穆贾雅
- 区块链精品电子书《人工智能时代,一本书读懂区块链金融 (互联网_时代企业管理实战系列)》-马兆林
-
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https'){
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else{
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试
var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!
©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任
来源:ZhiKuGroup博客,欢迎分享。
评论专区