串是由零个或者多个字符组成的有序序列,又叫字符串
串的逻辑结构和线性表相似,但是串针对的是字符集,也就是串中的元素都是字符,更关注的是查找子串的位置,得到,替换指定位置的子串,线性表更关注的是单个字符的操作,比如查找,插入,删除一个元素
串顺序存储结构是用一组地址连续的存储但愿来存储串种的字符序列的,按照预定义的大小,为每个定义的串变量分配一个固定长度的存储区,一般是用定长数组来定义
顺序串数据结构:
typedef struct {
char str[MAXSIZE];
int length;
}String;
今天继续完成所有操作,coredump扩了一晚上了,在VS2013上一直会出现,而gcc编译的运行却没这个问题~!
(ps:VS上修改成下面就好了)
pstr->str = (char *)malloc(sizeof(char));
char *a = “hello world!Congratulations!”;
pstr->str = a;
==========================================
#include <stdio.h>
#include <malloc.h>
//#define MAXSIZE 15
typedef struct {
char *str;
int length;
}String;
int StringLength(String *s){
int i;
i = 0;
while (s->str[i++] != ‘\0’){
s->length++;
}
return s->length;
}
String* CopyString(String *s, String *p){
if (*s->str == ‘\0’){
printf(“String pos error!\n”);
return 0;
}
int i, j;
i = 0;
j = 0;
while (s->str[i] != ‘C’){
p->str[j++] = s->str[i++];
p->length++;
}
p->str[j] = ‘\0’;
return p;
}
int main(){
String *pstr;
pstr = (String *)malloc(sizeof(String));
pstr->length = 0;
pstr->str = “hello world!Congratulations!”;
pstr->length = StringLength(pstr);
printf(“Length: %d\n”, pstr->length);
// printf(“%c”, *pstr->str);
String *copystr = (String *)malloc(sizeof(String));
copystr->length = 0;
copystr->str = (char *)malloc(sizeof(char));
CopyString(pstr, copystr);
printf(“After Copy:”);
printf(“%c”, *copystr->str);
while (*copystr->str++ != ‘\0’){
printf(“%c”, *copystr->str);
}
free(pstr);
free(copystr);
return 0;
}