字符串的基本操作

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





posted @ 2015-07-07 12:50  Lucas_1993  阅读(247)  评论(0编辑  收藏  举报