diff --git a/_03.串/_a.串.c b/_03.串/_a.串.c index 45fef31..cb083ac 100644 --- a/_03.串/_a.串.c +++ b/_03.串/_a.串.c @@ -2,7 +2,7 @@ * @Author: Xu Bai * @Date: 2019-07-06 22:20:08 * @LastEditors: Xu Bai - * @LastEditTime: 2019-07-07 23:21:21 + * @LastEditTime: 2019-07-07 23:58:05 */ #include "string.h" @@ -210,27 +210,108 @@ int index2(String S, String T, int pos) return 0; } +// ÔÚ´®sµÄµÚpos¸ö×Ö·û֮ǰ²åÈë´®T£¬ÍêÈ«²åÈëÔò·µ»ØTRUE +Status StrInsert(String S, int pos, String T) +{ + int i; + if (pos < 1 || pos > S[0] + 1) + { + return ERROR; + } + if (S[0] + T[0] <= MAXSIZE) + { + // ¿ÉÒÔÍêÈ«²åÈë + for (i = S[0]; i >= pos; i--) + { + S[i + T[0]] = S[i]; + } + for (i = pos; i < pos + T[0]; i++) + { + S[i] = T[i - pos + 1]; + } + S[0] = S[0] + T[0]; + return TRUE; + } + else + { + for (i = MAXSIZE; i <= pos; i--) + { + S[i] = S[i - T[0]]; // ´ÓºóÍùǰ + } + for (i = pos; i < pos + T[0]; i++) + { + S[i] = T[i - pos + 1]; + } + S[0] = MAXSIZE; + return FALSE; + } +} +// ´Ó´®SÖÐɾ³ýµÚpos¸ö×Ö·ûÆð³¤¶ÈΪlenµÄ×Ó´® +Status StrDelete(String S, int pos, int len) +{ + int i; + if (pos < 1 || pos > S[0] - len + 1 || len < 0) + { + return ERROR; + } + for (i = pos + len; i < S[0]; i++) + { + S[i - len] = S[i]; + } + S[0] -= len; + return OK; +} -// Status Replace(String S,String T, String V){ -// // ÓÃVÌæ»»Ö÷´®SÖгöÏÖµÄËùÓÐÓëTÏàµÈµÄ²»ÖصþµÄ×Ó´® -// int i =1; -// if(StrEmpty(T)){ -// return ERROR; -// } +// ÓÃVÌæ»»Ö÷´®SÖгöÏÖµÄËùÓÐÓëTÏàµÈµÄ²»ÖصþµÄ×Ó´® +Status Replace(String S, String T, String V) +{ -// do{ -// i= -// } -// } + int i = 1; + if (StrEmpty(T)) + { + return ERROR; + } + + do + { + i = Index(S, T, i); + // iΪ´ÓÉÏÒ»¸öiÖ®ºóÕÒµ½µÄ×Ó´®TµÄλÖà + if (i) + { + // ´®SÖдæÔÚT + StrDelete(S, i, StrLength(T)); + StrInsert(S, i, V); + i += StrLength(V); + //ÔÚ²åÈëµÄ´®Vºó¼ÌÐø²éÕÒ´®T + } + } while (i); + return OK; +} + +void StrPrint(String T) +{ + int i; + for (i = 1; i <= T[0]; i++) + { + printf("%c ", T[i]); + } + printf("\n"); +} int main() { - char chars[] = "ABCD"; - String T = " "; - StrAssign(T, chars); - printf("%s \n", T); - printf("%c", T[3]); + Status k; + String s1, s2; + k = StrAssign(s1, "bai_xu"); + if (!k) + { + printf("ERROR!\n"); + exit(0); + } + StrPrint("s1=%s \n",s1); + StrCopy(s2,s1); + printf("copyed s2=%s \n",s2); getchar(); return OK; diff --git a/a.out b/a.out index 84d5a09..86c558f 100644 Binary files a/a.out and b/a.out differ