1. #include "string.h"
    2. #include "stdio.h"
    3. #include "stdlib.h"
    4. #include "io.h"
    5. #include "math.h"
    6. #include "time.h"
    7. #define OK 1
    8. #define ERROR 0
    9. #define TRUE 1
    10. #define FALSE 0
    11. #define MAXSIZE 40 /* 存储空间初始分配量 */
    12. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
    13. typedef int ElemType; /* ElemType类型根据实际情况而定,这里假设为int */
    14. typedef char String[MAXSIZE+1]; /* 0号单元存放串的长度 */
    15. /* 生成一个其值等于chars的串T */
    16. Status StrAssign(String T,char *chars)
    17. {
    18. int i;
    19. if(strlen(chars)>MAXSIZE)
    20. return ERROR;
    21. else
    22. {
    23. T[0]=strlen(chars);
    24. for(i=1;i<=T[0];i++)
    25. T[i]=*(chars+i-1);
    26. return OK;
    27. }
    28. }
    29. /* 由串S复制得串T */
    30. Status StrCopy(String T,String S)
    31. {
    32. int i;
    33. for(i=0;i<=S[0];i++)
    34. T[i]=S[i];
    35. return OK;
    36. }
    37. /* 若S为空串,则返回TRUE,否则返回FALSE */
    38. Status StrEmpty(String S)
    39. {
    40. if(S[0]==0)
    41. return TRUE;
    42. else
    43. return FALSE;
    44. }
    45. /* 初始条件: 串S和T存在 */
    46. /* 操作结果: 若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0 */
    47. int StrCompare(String S,String T)
    48. {
    49. int i;
    50. for(i=1;i<=S[0]&&i<=T[0];++i)
    51. if(S[i]!=T[i])
    52. return S[i]-T[i];
    53. return S[0]-T[0];
    54. }
    55. /* 返回串的元素个数 */
    56. int StrLength(String S)
    57. {
    58. return S[0];
    59. }
    60. /* 初始条件:串S存在。操作结果:将S清为空串 */
    61. Status ClearString(String S)
    62. {
    63. S[0]=0;/* 令串长为零 */
    64. return OK;
    65. }
    66. /* 用T返回S1和S2联接而成的新串。若未截断,则返回TRUE,否则FALSE */
    67. Status Concat(String T,String S1,String S2)
    68. {
    69. int i;
    70. if(S1[0]+S2[0]<=MAXSIZE)
    71. { /* 未截断 */
    72. for(i=1;i<=S1[0];i++)
    73. T[i]=S1[i];
    74. for(i=1;i<=S2[0];i++)
    75. T[S1[0]+i]=S2[i];
    76. T[0]=S1[0]+S2[0];
    77. return TRUE;
    78. }
    79. else
    80. { /* 截断S2 */
    81. for(i=1;i<=S1[0];i++)
    82. T[i]=S1[i];
    83. for(i=1;i<=MAXSIZE-S1[0];i++)
    84. T[S1[0]+i]=S2[i];
    85. T[0]=MAXSIZE;
    86. return FALSE;
    87. }
    88. }
    89. /* 用Sub返回串S的第pos个字符起长度为len的子串。 */
    90. Status SubString(String Sub,String S,int pos,int len)
    91. {
    92. int i;
    93. if(pos<1||pos>S[0]||len<0||len>S[0]-pos+1)
    94. return ERROR;
    95. for(i=1;i<=len;i++)
    96. Sub[i]=S[pos+i-1];
    97. Sub[0]=len;
    98. return OK;
    99. }
    100. /* 返回子串T在主串S中第pos个字符之后的位置。若不存在,则函数返回值为0。 */
    101. /* 其中,T非空,1≤pos≤StrLength(S)。 */
    102. int Index(String S, String T, int pos)
    103. {
    104. int i = pos; /* i用于主串S中当前位置下标值,若pos不为1,则从pos位置开始匹配 */
    105. int j = 1; /* j用于子串T中当前位置下标值 */
    106. while (i <= S[0] && j <= T[0]) /* 若i小于S的长度并且j小于T的长度时,循环继续 */
    107. {
    108. if (S[i] == T[j]) /* 两字母相等则继续 */
    109. {
    110. ++i;
    111. ++j;
    112. }
    113. else /* 指针后退重新开始匹配 */
    114. {
    115. i = i-j+2; /* i退回到上次匹配首位的下一位 */
    116. j = 1; /* j退回到子串T的首位 */
    117. }
    118. }
    119. if (j > T[0])
    120. return i-T[0];
    121. else
    122. return 0;
    123. }
    124. /* T为非空串。若主串S中第pos个字符之后存在与T相等的子串, */
    125. /* 则返回第一个这样的子串在S中的位置,否则返回0 */
    126. int Index2(String S, String T, int pos)
    127. {
    128. int n,m,i;
    129. String sub;
    130. if (pos > 0)
    131. {
    132. n = StrLength(S); /* 得到主串S的长度 */
    133. m = StrLength(T); /* 得到子串T的长度 */
    134. i = pos;
    135. while (i <= n-m+1)
    136. {
    137. SubString (sub, S, i, m); /* 取主串中第i个位置长度与T相等的子串给sub */
    138. if (StrCompare(sub,T) != 0) /* 如果两串不相等 */
    139. ++i;
    140. else /* 如果两串相等 */
    141. return i; /* 则返回i值 */
    142. }
    143. }
    144. return 0; /* 若无子串与T相等,返回0 */
    145. }
    146. /* 初始条件: 串S和T存在,1≤pos≤StrLength(S)+1 */
    147. /* 操作结果: 在串S的第pos个字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE */
    148. Status StrInsert(String S,int pos,String T)
    149. {
    150. int i;
    151. if(pos<1||pos>S[0]+1)
    152. return ERROR;
    153. if(S[0]+T[0]<=MAXSIZE)
    154. { /* 完全插入 */
    155. for(i=S[0];i>=pos;i--)
    156. S[i+T[0]]=S[i];
    157. for(i=pos;i<pos+T[0];i++)
    158. S[i]=T[i-pos+1];
    159. S[0]=S[0]+T[0];
    160. return TRUE;
    161. }
    162. else
    163. { /* 部分插入 */
    164. for(i=MAXSIZE;i<=pos;i--)
    165. S[i]=S[i-T[0]];
    166. for(i=pos;i<pos+T[0];i++)
    167. S[i]=T[i-pos+1];
    168. S[0]=MAXSIZE;
    169. return FALSE;
    170. }
    171. }
    172. /* 初始条件: 串S存在,1≤pos≤StrLength(S)-len+1 */
    173. /* 操作结果: 从串S中删除第pos个字符起长度为len的子串 */
    174. Status StrDelete(String S,int pos,int len)
    175. {
    176. int i;
    177. if(pos<1||pos>S[0]-len+1||len<0)
    178. return ERROR;
    179. for(i=pos+len;i<=S[0];i++)
    180. S[i-len]=S[i];
    181. S[0]-=len;
    182. return OK;
    183. }
    184. /* 初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) */
    185. /* 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */
    186. Status Replace(String S,String T,String V)
    187. {
    188. int i=1; /* 从串S的第一个字符起查找串T */
    189. if(StrEmpty(T)) /* T是空串 */
    190. return ERROR;
    191. do
    192. {
    193. i=Index(S,T,i); /* 结果i为从上一个i之后找到的子串T的位置 */
    194. if(i) /* 串S中存在串T */
    195. {
    196. StrDelete(S,i,StrLength(T)); /* 删除该串T */
    197. StrInsert(S,i,V); /* 在原串T的位置插入串V */
    198. i+=StrLength(V); /* 在插入的串V后面继续查找串T */
    199. }
    200. }while(i);
    201. return OK;
    202. }
    203. /* 输出字符串T */
    204. void StrPrint(String T)
    205. {
    206. int i;
    207. for(i=1;i<=T[0];i++)
    208. printf("%c",T[i]);
    209. printf("\n");
    210. }
    211. int main()
    212. {
    213. int i,j;
    214. Status k;
    215. char s;
    216. String t,s1,s2;
    217. printf("请输入串s1: ");
    218. k=StrAssign(s1,"abcd");
    219. if(!k)
    220. {
    221. printf("串长超过MAXSIZE(=%d)\n",MAXSIZE);
    222. exit(0);
    223. }
    224. printf("串长为%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1));
    225. StrCopy(s2,s1);
    226. printf("拷贝s1生成的串为: ");
    227. StrPrint(s2);
    228. printf("请输入串s2: ");
    229. k=StrAssign(s2,"efghijk");
    230. if(!k)
    231. {
    232. printf("串长超过MAXSIZE(%d)\n",MAXSIZE);
    233. exit(0);
    234. }
    235. i=StrCompare(s1,s2);
    236. if(i<0)
    237. s='<';
    238. else if(i==0)
    239. s='=';
    240. else
    241. s='>';
    242. printf("串s1%c串s2\n",s);
    243. k=Concat(t,s1,s2);
    244. printf("串s1联接串s2得到的串t为: ");
    245. StrPrint(t);
    246. if(k==FALSE)
    247. printf("串t有截断\n");
    248. ClearString(s1);
    249. printf("清为空串后,串s1为: ");
    250. StrPrint(s1);
    251. printf("串长为%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1));
    252. printf("求串t的子串,请输入子串的起始位置,子串长度: ");
    253. i=2;
    254. j=3;
    255. printf("%d,%d \n",i,j);
    256. k=SubString(s2,t,i,j);
    257. if(k)
    258. {
    259. printf("子串s2为: ");
    260. StrPrint(s2);
    261. }
    262. printf("从串t的第pos个字符起,删除len个字符,请输入pos,len: ");
    263. i=4;
    264. j=2;
    265. printf("%d,%d \n",i,j);
    266. StrDelete(t,i,j);
    267. printf("删除后的串t为: ");
    268. StrPrint(t);
    269. i=StrLength(s2)/2;
    270. StrInsert(s2,i,t);
    271. printf("在串s2的第%d个字符之前插入串t后,串s2为:\n",i);
    272. StrPrint(s2);
    273. i=Index(s2,t,1);
    274. printf("s2的第%d个字母起和t第一次匹配\n",i);
    275. SubString(t,s2,1,1);
    276. printf("串t为:");
    277. StrPrint(t);
    278. Concat(s1,t,t);
    279. printf("串s1为:");
    280. StrPrint(s1);
    281. Replace(s2,t,s1);
    282. printf("用串s1取代串s2中和串t相同的不重叠的串后,串s2为: ");
    283. StrPrint(s2);
    284. return 0;
    285. }