1.定义及性质:
(1)串是一种特殊的线性表,它的数据元素仅由字符组成。空串是任意串的子串,任意串的子串是它本身符组成的子序列。
在c语言中,字符串用'\0'作为结尾
(2)子串:串中任意个连续字
(3)主串:包含子串的串
(4)位置索引(序号):子串的第一个元素在主串中的位置(从0开始)
(5)空串:长度为0的串,
(6)串值:用双引号括起来的字符序列
(7)串长:串中包含的字符个数 不包括最后的'\0’
2.存储实现
(1)循序存储(顺序串)
代码实现:
#definde MAXSIZE 1024
typedef struct{
char ch[MAXSIZE];
int len;
}SeqString;
(2)链式存储(链串)
代码实现:
#definde MAXSIZE 1024
typedef struct linknode{
char data[4];
struct linknode *next;
}LinkString;
(4)索引存储
代码实现:
//带长度的索引表
typedef struct{
char name[10];
int length;
struct *start;
}LenNode;
//带末指针的索引表
typedef struct{
char name[10];
char *start,*end;
}EndNode;
//带特征位的索引表
typedef struct{
char name[10];
int tag;//特征位
union{
char *start;
char value[4];
}uval;
}TagNode;
3.运算实现
(以顺序串来说,其他的请看我接下来的文章)
(1)初始化,将字符串常量的值变成T的值
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 1024
typedef char Datatype;
//顺序串
typedef struct{
Datatype ch[MAXSIZE];
int len;
}SeqString;
//初始化,将字符串常量的值变成T的值
SeqString * StrAssign(SeqString *T,char *chars){
int i=0;
T = (SeqString *)malloc(sizeof(SeqString));
T->len=0;
for(i=0;i<strlen(chars);i++){
T->ch[i]=chars[i];
T->len++;
}
T->ch[T->len]='\0';
return T;
}
(2)求串长
//求串长度Strlen
int StrLen(SeqString *S){
return S->len;
}
(3)字符串复制,把字符串T的值复制给S,StrCopy
//字符串复制,把字符串T的值复制给S,StrCopy
SeqString * StrCopy(SeqString *S,SeqString *T){
int i=0;
for(i=0;i<StrLen(T);i++){
S->ch[i]=T->ch[i];
}
S->len=T->len;
return S;
}
(4)判断串是否为空 StrEmpty
//判断串是否为空 StrEmpty
int StrEnmoty(SeqString *S){
if(StrLen(S)==0){
return 1;
}else{
return 0;
}
}
(5)判断大小,StrCmp,如果S1>S2,返回值>0...
根据ASCII码值去判断,从头开始当出现不相等的字符时候开始比较
//判断大小,StrCmp,如果S1>S2,返回值>0...
//根据ASCII码值去判断,从头开始当出现不相等的字符时候开始比较
int StrCmp(SeqString *S1,SeqString *S2){
int i=0;
for(i;i<StrLen(S1);i++){
if(S1->ch[i]!=S2->ch[i]){
if(S1->ch[i]>S2->ch[i]){
return 1;
}else{
return -1;
}
}
}
if(StrLen(S1)>StrLen(S2)){
return 1;
}
else if(StrLen(S1)==StrLen(S2)){
return 0;
}else{
return -1;
}
}
(6)字符串连接 StrCat,把S2的串放在S1末尾 变成一个新串
//字符串连接 StrCat,把S2的串放在S1末尾 变成一个新串
SeqString * StrCat(SeqString *S1,SeqString *S2){
int i=0;
SeqString *R;
if((StrLen(S1)+StrLen(S2))>=MAXSIZE-1){
printf("上溢!\n");
return NULL;
} else{
R = (SeqString *)malloc(sizeof(SeqString));
for(i=0;i<StrLen(S1);i++){
R->ch[i]=S1->ch[i];
}
for(i=0;i<StrLen(S2);i++){
R->ch[S1->len+i]=S2->ch[i];
}
R->ch[S1->len+S2->len+1]='\0';
R->len=S1->len+S2->len;
return R;
}
}
(7)求子字串,SubStr,从S1中i位置字符开始,向后抽出j个字符构成一个新的串
SeqString * SubStr(SeqString *S1,int i,int j){
SeqString *S2;
S2=(SeqString *)malloc(sizeof(SeqString));
int k=0;
if(i+j-1>StrLen(S1)){
printf("超界!");
return NULL;
}else{
for(k;k<j;k++){
S2->ch[k]=S1->ch[i+k-1];
}
S2->len=j;
S2->ch[S2->len]='\0';
return S2;
}
}
(8)字符串定位StrIndex,返回S2在S1的pos位置之后,出现的下标第一个位置
模式匹配
//字符串定位StrIndex,返回S2在S1的pos位置之后,出现的下标第一个位置
//模式匹配
int StrIndex(SeqString *S1,SeqString *S2,int pos){
int i=pos,j=0;
while(j<S2->len&&i<S1->len){
if(S2->ch[j]==S1->ch[i]){
i++;
j++;
}
else{
j=0;
i=i-j+1;//下一次位置
}
}
if(j==StrLen(S2)){
return i-StrLen(S2);
}else{
return -1;
}
}
(9)换Replace,S1中下标从start-end的子串用S2代替
SeqString *Replace(SeqString *S1,SeqString *S2,int start,int end){
SeqString *s1,*s2;
s1 = (SeqString *)malloc(sizeof(SeqString));
s2 = (SeqString *)malloc(sizeof(SeqString));
int i=0,j=0,k=0;
for(i;i<StrLen(S1);i++){
if(i<start){
s1->ch[j]=S1->ch[i];
j++;
}else if(i>end){
s2->ch[k]=S1->ch[i];
k++;
}
}
s1->len=j;
s2->len=k;
S1=StrCat(s1,S2);
S1=StrCat(S1,s2);
free(s1);
free(s2);
return S1;
}
(10)StrInsert,在串S的第pos个字符之前插入串T
SeqString *StrInsert(SeqString *S,int pos,SeqString *T){
if(pos<0||pos>StrLen(S)){
printf("超出范围!");
return NULL;
}else{
SeqString *s1,*s2;
s1 = (SeqString *)malloc(sizeof(SeqString));
s2 = (SeqString *)malloc(sizeof(SeqString));
int i=0,j=0,k=0;
for(i;i<StrLen(S);i++){
if(i<pos-1){
s1->ch[j]=S->ch[i];
j++;
}else{
s2->ch[k]=S->ch[i];
k++;
}
}
s1->len=j;
s2->len=k;
S=StrCat(s1,T);
S=StrCat(S,s2);
free(s1);
free(s2);
return S;
}
}
(11)StrDelete字符串删除,从串S删除第pos个位置开始长度为len个字符
SeqString *StrDelete(SeqString *S,int pos,int len){
if(pos<0||pos+len>StrLen(S)) {
printf("超出范围!");
return NULL;
}else{
SeqString *s1,*s2;
s1 = (SeqString *)malloc(sizeof(SeqString));
s2 = (SeqString *)malloc(sizeof(SeqString));
int i=0,j=0,k=0;
for(i;i<StrLen(S);i++){
if(i<pos){
s1->ch[j]=S->ch[i];
j++;
}else if(i>=pos+len){
s2->ch[k]=S->ch[i];
k++;
}
}
s1->len=j;
s2->len=k;
S=StrCat(s1,s2);
return S;
}
}
(关于字符串匹配有更好的办法,KMP算法)