串的定長順序儲存C語言實現

2021-06-21 16:09:49 字數 3498 閱讀 9821

串(字串)是由0個或多個字元組成的有限序列。0個字元時稱為空串。由乙個或多個空格組成的串『 』稱為空格串。串中字元的數目n稱為串的長度;串中任意個連續的字元組成的子串行稱為該串的字串;包含字串的串相應的稱為主串;通常稱字元在序列中的序號稱為該字元在串中的位置。字串在主串中的位置則以字串的第乙個字元在主串中的位置來表示。串相等:只有兩個串的長度相等,並且各個對應位置的字元都相等時才相等。

串的操作中,通常是以「串的整體」作為操作物件的。

基本操作如下(紅色標註為串的最小操作子集):

// chars是字串常量,生成乙個其值等於chars的串t

strassign(&t, chars)

// 串s存在,由串s複製得串t

strcopy(&t, s)

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

strempty(s)

// 串s和t存在,若s > t,則返回值 > 0;若s = t,則返回值 = 0;若s < t,則返回值 < 0

strcompare(s, t)

// 串s存在,返回s的元素個數,稱為串的長度

strlength(s)

// 串s存在,將s清為空串

clearstring(&s)

// 串s1和s2存在,用t返回由s1和s2聯接而成的新串

concat(&t, s1, s2)

// 串s存在,1 <= pos <= strlength(s) 且 0 <= len <= strlength(s) - pos + 1,用sub返回串s的第pos個字元起長度為len的字串

substring(&sub, s, pos, len)

// 串s和t存在,t是非空串,1 <= pos <= strlength(s) ,若主串s中存在和串t值相同的字串,則返回它的主串s中第pos個字元之後第一次出現的位置;否則函式值為0

inxdex(s, t, pos)

// 串s,t和v存在,t是非空串;用v替換主串s中出現的所有與t相等的不重疊的字串

replace(&s, t, v)

// 串s和t存在, 1 <= pos <= strlength(s) + 1;在串s的第pos個字元之前插入串t。

strinsert(&s, pos, t)

// 串s存在, 1 <= pos <= strlength(s) - len +1 ;從串s中刪除第pos個字元起長度為len的字串

strdelete(&s, pos, len)

// 串s存在, 銷毀串s

destroystring(&s)

#include 

#include

#include

#define true 1

#define false 0

#define maxstrlen 255

unsigned char sstring[maxstrlen + 1];

typedef int status;

status strassign(unsigned char t, char *chars);

status strlength(unsigned char s);

status strcopy(unsigned char t, unsigned char s);

status strempty(unsigned char s);

status strcompare(unsigned char t, unsigned char s);

status concat(unsigned char t, unsigned char s1, unsigned char s2);

status substring(unsigned char sub, unsigned char s, int pos, int len);

status index(unsigned char s, unsigned char t, int pos);

//status clearstring(&s);

//status replace(&s, t, v);

//status strinsert(&s, pos, t);

//status strdelete(&s, pos, len);

//status destroystring(&s);

/* * @:t : the string you want to creat

* @:char *: the string

* return true(success), false(failure)

*/status strassign(unsigned char t, char *chars)

else

return true; }}

/* *@s:the length you want to test

*return the length of s

*/status strlength(unsigned char s)

/* *@s:the string you want to test

*return true:the string is empty

* false:the string is not empty

*/status strempty(unsigned char s)

/* * @t: the string you want to copy to

* @s: the string you want to copy

*/status strcopy(unsigned char t, unsigned char s)

/* *@t s : the strings you want to compare

*return 0:t=s

* <0:t

0:t>s

*/status strcompare(unsigned char t, unsigned char sstring) }

return (sstring[0] - t[0]);}/*

* */

status concat(unsigned char t, unsigned char s1, unsigned char s2)

for(i = 1; i<= s2[0]; i++)

return true;

}else

for(i = 1; i <= maxstrlen-s1[0]; i++)

return false;

} }status substring(unsigned char sub, unsigned char s, int pos, int len)

sub[0] = len;

return true;

}status index(unsigned char s, unsigned char t, int pos)

} return true;

}int

main(void)

定長順序串 C語言

題目 用定長順序串編寫下列演算法 1.將順序串r中所有值為ch1的字元轉換成ch2的字元。2.將順序串r中所有字元按照相反次序仍存放在r中。3.從順序串r中刪除其值等於ch的所有字元。4.從順序串r1中第index個字元起求首次與串r2相同的子串的起始位置。5.從順序串r中刪除所有與串r1相同的子串...

資料結構c語言實現定長順序串

標頭檔案 ifndef slhead h included define slhead h included include include include define maxlen 255 typedef char sstring maxlen 1 int strassign sstring s...

串的定長順序儲存表示與實現(c語言)

include pch.h include include include include define maxlen 225 define false 0 define true 1 字串順序表示 靜態儲存 typedef struct sstring 構造串 intstrassign sstri...