学习参考: 严蔚敏: 《数据结构-C语言版》
基本操作
- 赋值操作
- 字符串连接
- 取长度
- 字符串比较
- 求子串
- 输出字符串
- 清空操作
代码实现
结构定义:
typedef struct
{
char* ch;
int length;
}*pStr, DynString;
赋值操作
int strAssign(pStr str, char* ch)
{
int i = 0, len = 0;
char* c = ch;
if(str->ch)
free(str->ch);
str->ch = NULL;
str->length = 0;
while(*ch)
{
++len;
++ch;
}
if(len==0)
return 0;
str->ch = (char*)malloc(sizeof(char)*(len+1));
if(str->ch==NULL)
return 0;
for(i; i<len; ++i, ++c)
{
str->ch[i] = *c;
str->length++;
}
return 1;
}
字符串连接
int strCompare(pStr s1, pStr s2)
{
int i =