資料結構 串

2021-08-14 20:33:48 字數 3148 閱讀 6137

串:

串(string)是由零個或多個字元組成的有限序列,又名叫字串。 

串一般記為。用雙引號或單引號括起來的字串行是串的值(引號不屬於串的內容)。所謂的序列,說明串的相鄰字元之間具有前驅和後繼的關係。 

幾種特殊的串 

空串(nullstring):零個字元的串,它的長度為零,可以直接用兩雙引號「""」表示,也可以用希臘字母「φ」來表示。

空格串:只包含空格的串。 

子串與主串:串中任意個數的連續字元組成的子串行稱為該串的子串,相應地,包含子串的串稱為主串。子串在主串中的位置就是子串的第乙個字元在主串中的序號。

串的比較是通過組成串的字元之間的編碼來進行的,而字元的編碼指的是字元在對應字符集中的序號。 

目前計算機常用的編碼方式為標準的ascii編碼,由7位二進位制數表示乙個字元,總共可以表示128個字元。後來發現一些特殊符號的出現,128個不夠用,於是擴充套件ascii碼由8位二進位制數表示乙個字元,總共可以表示256個字元,這已經足夠滿足以英語為主的語言和特殊符號進行輸入、儲存、輸出等操作的字元需要了。 

全世界有成百上千種語言與文字,顯然這256個字元是不夠的,因此後來就有了unicode編碼,比較常用的是由16位的二進位制數表示乙個字元,這樣總共就可以表示

個字元,約是6.5萬多個字元,足夠表示世界上所有語言的所有字元了。為了和ascii碼相容,unicode的前256個字元與ascii碼完全相同。

串的比較是通過組成串的字元之間的編碼來進行的,而字元的編碼指的是字元在對應字符集中的序號。 

串的基本操作**如下:

標頭檔案:

#define size 100

typedef struct str

str;

//功能函式

void strassign(str*s, const char *chars);///

void strcpy(str*s, str *t);

void show(str *s);

bool isempty(str *s);//

int getlength(str*s);

void clear(str*s);

bool substr(str*sub, str*s, int pos, int len);//從s裡面的第pos位置提取長度為len的子串,放到sub中。空白不提取

bool insert(str*s, int pos, str*t);//在pos位置插入t

int bf(str*s, str*sub, int pos);

bool deletepos(str*s, int pos, int len);//從pos位置開始刪除len長度

bool delete(str*s, int pos, str*t);//從pos位置刪子串t

bool replace(str*s, str *t, str*v, int pos);//用v替換從pos位置開始的第乙個

bool replaceall(str *s, str *t, str*v);

str.cpp

#include

#include

#include

#include"str.h"

void strassign(str *s,const char *chars)//初始化

for (int i = 0; i < len; i++)

s->length = len; }

void strcpy(str *s, str*t)//串t 拷貝到s

s->length = t->length; }

void show(str *s)//show

printf("\n"); }

bool isempty(str*s)//判空

int getlength(str*s)//串長度

void clear(str*s)//清空

bool substr(str*sub, str*s, int pos, int len)//從s裡面的第pos位置提取長度為len的子串,放到sub中。且空白不提取

for (int i =0; i <= len;i++)

sub->length = len;

return true; }

bool insert(str*s, int pos, str*t)//在pos位置插入字串t

for (int i =lens-1; i > pos ; --i)//挪資料

for (int i = 0; i < lent; i++)

s->length += t->length; }

int bf(str*s, str*sub, int pos)//

int i = pos;

int j = 0;

int lens = getlength(s);

int lensub = getlength(sub);

while (i < lens&&j < lensub)

else

} if (j >= lensub)

else

} bool deletepos(str*s, int pos, int len)//從pos位置開始刪除len長度

for (int i = pos; i < s->length - len; i++)

s->length -= len;

return true; }

bool delete(str*s, int pos, str*t)//從pos位置刪子串t

return deletepos(s, index, t->length); }

bool replace(str*s, str *t, str*v, int pos)//用v替換從pos位置開始的第乙個

deletepos(s, index, t->length);

return insert(s, index, v); }

bool replaceall(str *s, str *t, str*v)//全替換

測試用例:

test.cpp

#include

#include

#include"str.h"

int main()

執行結果:

資料結構 串

輸入乙個字串,按字典序列印出該字串中字元的所有排列。例如輸入字串abc,則列印出由字元a,b,c所能排列出來的所有字串abc,acb,bac,bca,cab和cba。結果請按字母順序輸出。我們求整個字串的排列,可以看成兩步 首先求所有可能出現在第乙個位置的字元,即把第乙個字元和後面所有的字元交換。第...

資料結構 串

串名 串值 串長 空串 空格串。字串的比較 字串模式匹配演算法 1.簡單字串模式匹配演算法 最簡單實現是用字串p的字元依次與字串t中的字元進行比較。實現思想是,首先將子串p從第0個字元起與主串t的第pos個字元起依次比較對應字元,如全部對應相等,則表明已找到匹配,成功終止。否則將子串p從第0個子串起...

資料結構 串

串 又稱字串 是一種特殊的線性表,它的每個結點僅由乙個字元組成。在早期的程式語言中,串僅在輸入或輸出中以直接量的形式出現,並不參與運算。隨著計算機的發展,串在文字編輯 詞法掃瞄 符號處理以及定理證明等許多領域得到越來越廣泛的應用。在高階語言中開始引入了串變數的概念,如同整型 實型變數一樣,串變數也可...