/*
* 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
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] - s2.ch[i];
i ++;
}
return s1.length - s2.length;
}
//取得串长度,串中字符的个数称为串的长度
int hstringlength(hstring s) {
return s.length;
}
//清空字符串并释放串的所有空间
void hstringclear(hstring *s) {
if(s->ch) {
free(s->ch);
s->length = 0;
s->ch = NULL;
}
}
//将s1和s2链接为一个串
int hstringconnection(hstring *s,hstring s1,hstring s2) {
if(s->ch) free(s->ch);
s->length = s1.length + s2.length;
s->ch = (char *)malloc(sizeof(char) * (s->length + 1));
if(s->ch == NULL) return FALSE;
int i = 0;
for(i = 0;i < s1.length;i ++) {
s->ch[i] = s1.ch[i];
}
for(;i < s->length;i ++) {
s->ch[i] = s2.ch[i - s1.length];
}
s->ch[i] = '\n';
return TRUE;
}
//从串中截取一段串截取规则是从first开始,从end前一个结束
int hstringsub(hstring *s,hstring hs,int first,int end) {
if(s->ch != NULL) free(s->ch);
if(first < 1 || end > hs.length + 1 || hs.length == 0) return FALSE;
s->ch = (char *)malloc(sizeof(char) * (end - first + 1));
if(s->ch == NULL) return FALSE;
s->length = end - first;
int i = 0;
for(i = 0;i < s->length;i ++) {
s->ch[i] = hs.ch[first + i - 1];
}
s->ch[s->length] = '\n';
return TRUE;
}
/*
* 串匹配得到子串t在串s中pos个字符后的开始位置
* 0< pos <= s->length
* 若pos后存在字串返回其位置 否则返回0
*/
int hstringindex(hstring s,hstring t,int pos) {
if(pos < 0 || pos >= s.length) return 0;
int i = pos,j = 0;
while(i < s.length && j < t.length) {
if(s.ch[i] == t.ch[j]) {
i ++;
j ++;
} else {
i = i - j + 1;
j = 0;
}
if(j == t.length) return i - j + 1;
}
return 0;
}
//输出串
void printfhstring(hstring *s) {
if(s->length == 0) {
printf("空串\n");
return;
}
puts(s->ch);
printf("length = %d\n",s->length);
}
/*
* hstring.c
* 测试heap_string.h
* Created on: 2011-9-7
* Author: root
*/
#include
#include
#include "heap_string.h"
int main(void) {
hstring hs;
//测试创建字符串
inithstring(&hs);
createhstring(&hs,"liu sheng");
printfhstring(&hs);
//测试比较字符串大小
hstring hs2;
inithstring(&hs2);
createhstring(&hs2,"liu_sheng");
printfhstring(&hs2);
int compare = hstringcompare(hs,hs2);
printf("compare = %d\n",compare);
//测试合并字符串
hstring t;
inithstring(&t);
hstringconnection(&t,hs,hs2);
printfhstring(&t);
//测试截取字符串
printfhstring(&hs);
hstringsub(&t,hs,5,9);
printfhstring(&t);
//测试求字串的位置
int index = hstringindex(hs,t,4);
printf("%d\n",index);
return 0;
}
//控制台结果
liu sheng
length = 9
liu_sheng
length = 9
compare = 0
liu shengliu_sheng
length = 18
liu sheng
length = 9
shen
length = 4
5