【数据结构】4.1串的基本操作(附代码实现)
2018-08-18 18:37
465 查看
串的基本操作有:
- 赋值
- 连接
- 比较
- 清空
- 求子串
具体代码如下
#include <stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> typedef struct String{ char * ch; int length; }Str; void StrInit(Str &str);//有多个由结构体创造出来的变量时(str1,str2,str3),不初始化很危险! int StrAssign(Str &str ,char * ch) ;//赋值 int GetStrLength(Str str);//获取长度 int StrCompare(Str s1,Str s2);//比较 int Concat(Str str1, Str str2, Str &str);//连接 int GetSubString(Str str,int pos, int len, Str &substr);//求子串 int ClearString(Str &str); //清空 int main(void) { Str str1, str2, str3,substr; StrInit(str1); StrInit(str2); StrInit(str3); StrInit(substr); char ch1[] = "I love China!"; char ch2[] = "I love U!"; char ch3[] = "Data structure!"; StrAssign(str1, ch1); StrAssign(str2, ch2); StrAssign(str3, ch3); Concat(str1,str2,str3) ; GetSubString(str3,3,7,substr); printf("%s\n",str1.ch); printf("%s\n",str2.ch); printf("%s\n",str3.ch); printf("%s\n",substr.ch); return 0; } void StrInit(Str &str) { str.length = 0; str.ch = NULL; } int StrAssign( Str & str ,char * ch) { //赋值 if(str.ch) //如果str.ch原先有存其他东西,就释放掉原先的东西 { free(str.ch); str.ch = NULL; } int len = 0; char * c = ch; while (*c) { //求串长 ++len; ++c; } if(len == 0) //空串 { str.ch = NULL; str.length = 0; return 1; } else { str.ch = (char *)malloc(sizeof(char)*(len + 1)); if(str.ch ==NULL)//空间分配失败 return 0; else {//写法1 c = ch; for(int i = 0; i <= len ; ++i,++c) str.ch[i] = *c; str.length = len; return 1; } // {//写法2 // i = 0; // c = ch; // while(c*) // { // str.ch[i] = c*; // ++c; // ++i; // } // str.[++i] = '\0'; // str.length = i; // } } } int GetStrLength(Str str) { return str.length; } int StrCompare(Str str1,Str str2) { //串比较 for(int i = 0; i <str1.length && i < str2.length; ++i) if(str1.ch[i] != str2.ch[i]) return str1.ch[i] - str2.ch[i]; return str1.length - str2.length; } int Concat(Str str1, Str str2, Str &str) { //串连接 if(str.ch) { free(str.ch); str.ch = NULL; } str.ch = (char *)malloc(sizeof(char)*(str1.length + str2.length + 1)); if(!str.ch) //空间分配失败 return 0; int i = 0,j = 0; while (i <str1.length) { str.ch[i] = str1.ch[i]; ++i; } while(j < str2.length) { str.ch[i + j] = str2.ch[j]; ++j; } str.ch[i+j] = '\0'; str.length = str1.length + str2.length; return 1; } int GetSubString(Str str,int pos, int len, Str &substr) { //求子串:从主串str里的第pos位置后开始,依次把len个元素作为子串substr if(pos < 0 || pos >=str.length || len < 0 || len > str.length - pos) return 0; if(substr.ch) { free(substr.ch); substr.ch == NULL; } if(len == 0) { substr.ch = NULL; substr.length = 0; return 1; } else { substr.ch = (char*)malloc(sizeof(char) * (len + 1)); int i = pos, j = 0; while(i < pos + len) { substr.ch[j] = str.ch[i]; ++i; ++j; } substr.ch[j] = '\0'; substr.length = len; return 1; } } int ClearString(Str & str) { //清空串 if(str.ch) { free(str.ch); str.ch == NULL; } str.length = 0; return 1; }阅读更多
相关文章推荐
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 用java实现单链表结构与基本数据操作
- c语言实现单链表数据结构及其基本操作
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- 【数据结构实验】实验二 顺序表的基本操作的实现
- 数据结构-顺序线性表基本操作实现
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- 数据结构之二叉排序树(基于指针实现基本操作)
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- 数据结构—链表-单链表基本操作实现