BCPL BasicCombinedProgammingLanguage即C语言实现
动态串及基本操作实现
#include <stdlib.h>
#include <stdio.h>
typedef struct{
char* ch;
int length;
}HString;
//赋值
int strAssign(HString *s, char* chars){
int len=0,i;
if(s->ch != NULL)
free(s->ch); //释放原有空间
while(chars[len] != '/0'){
len++;//求得char数组的长度
}
if(!len){//空串
s->ch = NULL;
s->length = 0;
}
else{
s->ch = (char*)malloc(len * sizeof(char));
if(!s->ch)
exit(-1);
for(i=0;i<len;i++){
s->ch[i] = chars[i];
s->length = len;
}
}
return 1;
}
//clear string
int clearString(HString *s){
if(s->ch)
free(s->ch);
s->ch = NULL;
s->length = 0;
return 1;
}