/*
* heap_string.h
* 串的堆分配存储实现,用这种实现方法的好处是,能够动态的给
* 串分配内存空间,而顺序串不能
* Created on: 2011-9-7
* Author: root
*/
#define ElemType char;
#define TRUE 1
#define FALSE 0
typedef struct {
char *ch;
int length;
}hstring;
//初始化串
void inithstring(hstring *s) {
s->length = 0;
s->ch = NULL;
}
//用字符串常量创建串
int createhstring(hstring *s,char chars[]) {
if(s->ch) free(s->ch);
int i=0;
while(chars[i]) {
i ++;
}
if(i == 0) return FALSE;
s->length = i;
s->ch = (char *)malloc(sizeof(char) * s->length);
if(s->ch == NULL) return FALSE;
for(i = 0;i < s->length;i ++) {
s->ch[i] = chars[i];
}
s->ch[i ++] = '\0';
return TRUE;
}
//比较两个串是否相等若s1>s2返回值>0若s1<s2返回值<0若s1=s2返回0
int hstringcompare(hstring s1,hstring s2) {
int i = 0;
while(s1.ch[i] && s2.ch[i]) {
if(s1.ch[i] - s2.ch[i] > 0) return s1.ch[i] -
C语言实现串的堆分配存储
最新推荐文章于 2021-09-10 02:00:29 发布
本文介绍了如何使用C语言实现堆分配存储的字符串结构,包括初始化、创建、比较、获取长度、清除、连接、子串截取和查找子串等功能。通过示例代码展示了这些操作的使用。
摘要由CSDN通过智能技术生成