数据结构学习笔记:串
来源:互联网 发布:淘宝如何出售虚拟物品 编辑:程序博客网 时间:2024/06/03 18:34
串(string)是由零个或者多个字符组成的有限序列,也称为字符串。
空串(null string)直接用两双引号表示。
空格串,只包含空格的串。空格串是有内容有长度的,而且不止一个空格。
子串与主串,串中任意个数的连续字符组成的子序列称为该串的子串,相应地,包含子串的串,称为主串。
串的比较是通过组成串的字符之间的编码来进行的,而字符的编码指的是字符在对应字符集中的序号。
//定长顺序存储typedef struct{char str[maxSize + 1]; //预留辅助变量存储空间。在0位置存储长度,或者在后边使用 '\0'结束标记符int length;}Str;//变长分配存储typedef struct{char *ch; //指向动态分配存储区首地址的字符指针int length;}Str;
//赋值操作int strAssign( Str& my_str, char* ch){if ( my_str.ch ){free( my_str.ch); //释放原串空间}int len = 0;char *c = ch;while( *c )//计算ch串的长度{++ len;++ c;}if( 0 == len ){ my_str.ch = NULL; my_str.length = 0;return true;}else{ my_str.ch = (char *) malloc ( sizeof ( char*) * (len + 1));//取len + 1, 是为了多分配 ‘\0’空间if( NULL == my_str.ch ){return FALSE;}else{c = ch;for ( int i = 0; i <= len; ++ i, ++ c ){ my_str.ch[i] = *c;} my_str.length = len;return true;}}}//使用格式strAssign( Str, "CSDN Good"); // 结果是 str.ch 值为 “CSDN Good”, str.length = 9
// 串比较操作int strCompare(Str s1, Str s2){for ( int i = 0; i < s1.length && i < s2.length; ++ i){if ( s1.ch[i] != s1.ch[i]){return ( s1.ch[i] - s1.ch[i] );}}return s1.length - s2.length;}//串连接操作int strConcat(Str& my_str, Str s1, Str s2){if (my_str.ch){free(my_str.ch);my_str.ch = NULL;}my_str.ch = (char *) malloc( sizeof(char) * (s1.length + s2.length + 1));if (NULL == my_str.ch){return false;}int i = 0;while( i < s1.length ){my_str.ch[i] = s1.ch[i];++ i;}int j = 0;while( j <= s2.length )//要连同s2最后的‘\0’一起复制{my_str.ch[i+j] = s2.ch[j];++ j; }my_str.length = s1.length + s2.length;return true;}//求子串操作int subString(Str& substr, Str s, int pos, int len){if ( pos < 0 || pos >= s.length || len < 0 || len > s.length - pos ){return false;}if (substr.ch){free(substr.ch);substr.ch = NULL;}if (0 == len){substr.ch = NULL;substr.length = 0;return true;}else{substr.ch = (char *) malloc( sizeof(char) * (len + 1));int i = pos;int j = 0;while ( i < pos + len ){substr.ch[j] = s.ch[i];++i;++j;}substr.ch[j] = NULL;return true;}}//串清空操作int clearString(Str& s){if (s.ch){free(s);s.ch = NULL;}s.length = 0;return true;}
0 0
- 数据结构学习笔记:串
- 【学习笔记----数据结构08-串】
- 【数据结构】学习数据结构的学习笔记之串
- 严蔚敏数据结构学习笔记四.串
- 数据结构学习笔记--串的模式匹配
- 严蔚敏版数据结构学习笔记(5):串
- 数据结构学习笔记
- 数据结构学习笔记(一)
- 数据结构学习笔记--前言
- 数据结构学习笔记--迷宫
- 数据结构学习笔记
- Java数据结构 学习笔记
- 数据结构学习笔记
- 数据结构学习笔记
- 数据结构学习笔记
- 数据结构学习笔记1
- 数据结构学习笔记一
- 数据结构学习笔记二
- Android面试过程描述
- 进制转换
- 【数论】HDU 4952 Number Transformation
- hdu1269 迷宫城堡
- c++学习笔记之变量
- 数据结构学习笔记:串
- 【强烈推荐】《剑指Offer:名企面试官精讲典型编程题》一书中IT名企经典面试题
- 学习LUA--零星记录
- Altium Designer图文安装教程
- EasyUi dialog 去除默认关闭
- pelco-d协议数据解析示例
- LeetCode 77 Combinations
- GDI编程 之二、设备描述表DC
- 《创业宣言》