資料結構 字串

2021-07-22 11:34:34 字數 4374 閱讀 8308

1、字串

#include "string.h"

#include "stdio.h"

#include "stdlib.h"

//#include "math.h"

//#include "time.h"

#define ok 1

#define error 0

#define true 1

#define false 0

#define maxsize 40 /* 儲存空間初始分配量 */

typedef int status; /* status是函式的型別,其值是函式結果狀態**,如ok等 */

typedef int elemtype; /* elemtype型別根據實際情況而定,這裡假設為int */

typedef char string[maxsize + 1]; /* 0號單元存放串的長度 */

/* 生成乙個其值等於chars的串t */

status strassign(string t, char *chars)

}/* 由串s複製得串t */

status strcopy(string t, string s)

/* 若s為空串,則返回true,否則返回false */

status strempty(string s)

/* 初始條件: 串s和t存在 */

/* 操作結果: 若s>t,則返回值》0;若s=t,則返回值=0;若st長度呢?

/* 用sub返回串s的第pos個字元起長度為len的子串。 */

status substring(string sub, string s, int pos, int len)

/* 返回子串t在主串s中第pos個字元之後的位置。若不存在,則函式返回值為0。 */

/* 其中,t非空,1≤pos≤strlength(s)。 */

int index(string s, string t, int pos)

else /* 指標後退重新開始匹配 */

}if (j > t[0])

return i - t[0];

else

return 0;

}/* t為非空串。若主串s中第pos個字元之後存在與t相等的子串, */

/* 則返回第乙個這樣的子串在s中的位置,否則返回0 */

int index2(string s, string t, int pos)

} return 0; /* 若無子串與t相等,返回0 */

}/* 初始條件: 串s和t存在,1≤pos≤strlength(s)+1 */

/* 操作結果: 在串s的第pos個字元之前插入串t。完全插入返回true,部分插入返回false */

status strinsert(string s, int pos, string t)

/* 初始條件: 串s,t和v存在,t是非空串(此函式與串的儲存結構無關) */

/* 操作結果: 用v替換主串s中出現的所有與t相等的不重疊的子串 */

status replace(string s, string t, string v)

} while (i);

return ok;

}/* 輸出字串t */

void strprint(string t)

int main()

printf("串長為%d 串空否?%d(1:是 0:否)\n", strlength(s1), strempty(s1));

strcopy(s2, s1);

printf("拷貝s1生成的串為: ");

strprint(s2);

printf("請輸入串s2: ");

k = strassign(s2, "efghijk");

if (!k)

i = strcompare(s1, s2);

if (i<0)

s = '<';

else if (i == 0)

s = '=';

else

s = '>';

printf("串s1%c串s2\n", s);

k = concat(t, s1, s2);

printf("串s1聯接串s2得到的串t為: ");

strprint(t);

if (k == false)

printf("串t有截斷\n");

clearstring(s1);

printf("清為空串後,串s1為: ");

strprint(s1);

printf("串長為%d 串空否?%d(1:是 0:否)\n", strlength(s1), strempty(s1));

printf("求串t的子串,請輸入子串的起始位置,子串長度: ");

i = 2;

j = 3;

printf("%d,%d \n", i, j);

k = substring(s2, t, i, j);

if (k)

printf("從串t的第pos個字元起,刪除len個字元,請輸入pos,len: ");

i = 4;

j = 2;

printf("%d,%d \n", i, j);

strdelete(t, i, j);

printf("刪除後的串t為: ");

strprint(t);

i = strlength(s2) / 2;

strinsert(s2, i, t);

printf("在串s2的第%d個字元之前插入串t後,串s2為:\n", i);

strprint(s2);

i = index(s2, t, 1);

printf("s2的第%d個字母起和t第一次匹配\n", i);

substring(t, s2, 1, 1);

printf("串t為:");

strprint(t);

concat(s1, t, t);

printf("串s1為:");

strprint(s1);

replace(s2, t, s1);

printf("用串s1取代串s2中和串t相同的不重疊的串後,串s2為: ");

strprint(s2);

return 0;

}

2、kmp演算法

#include "string.h"

#include "stdio.h"

#include "stdlib.h"

//#include "io.h"

//#include "math.h"

//#include "time.h"

#define ok 1

#define error 0

#define true 1

#define false 0

#define maxsize 100 /* 儲存空間初始分配量 */

typedef int status; /* status是函式的型別,其值是函式結果狀態**,如ok等 */

typedef int elemtype; /* elemtype型別根據實際情況而定,這裡假設為int */

typedef char string[maxsize + 1]; /* 0號單元存放串的長度 */

/* 生成乙個其值等於chars的串t */

status strassign(string t, char *chars)

}status clearstring(string s)

/* 輸出字串t。 */

void strprint(string t)

/* 輸出next陣列值。 */

void nextprint(int next, int length)

/* 返回串的元素個數 */

int strlength(string s)

/* 樸素的模式匹配法 */

int index(string s, string t, int pos)

else /* 指標後退重新開始匹配 */

}if (j > t[0])

return i - t[0];

else

return 0;

}/* 通過計算返回子串t的next陣列。 */

void get_next(string t, int *next)

/* 求模式串t的next函式修正值並存入陣列nextval */

void get_nextval(string t, int *nextval)

int main()

資料結構 字串

字串是由0個或多個字元構成的序列,可記為s a1a2a3 an 其中ai可以是字母,也可是數字或者其他字元,零個字元的串稱為空串。而字串的順序結構就是用簡單的char型別陣列來儲存沒什麼好說的,下面介紹一下bf演算法與kmp演算法 bf演算法就是比較平常的雙重迴圈,如果匹配成功打斷迴圈,否則子串的比...

資料結構 字串

靜態陣列實現 順序儲存 串的順序儲存 define maxlen 255 預定義最大串長為255 typedef struct 靜態陣列實現 順序儲存 sstring 動態陣列實現 堆分配儲存 typedef struct 動態陣列實現 堆分配儲存 hstring 初始化void inithstri...

資料結構 字串匹配

演算法 如下,包括暴力匹配和kmp演算法。參考 include stringmatching.h stringmatching stringmatching void stringmatching stringmatching void 返回子串t在主串s中第pos個字串之後的位置。若不存在,則返回...