本文共 4497 字,大约阅读时间需要 14 分钟。
typedef struct { char *ch; int length;} HStirng; bool StrAssign(&T, chars) { // 释放已有内存 if (T.ch) { delete[] T.ch; } // 初始化长度 T.length = 0; // 计算字符串长度 char *ctemp = chars; while (*ctemp) { ++T.length; ++ctemp; } // 分配内存 T.ch = new char[T.length + 1]; if (!T.ch) { printf("分配失败!"); system(pause); exit(-1); } else { char *temp = T.ch; while (*chars) { *temp++ = *chars++; } printf("分配成功!"); return true; }} bool StrCopy(&T, S) { // 检查源字符串是否存在 if (!S.ch) { printf("串S不存在!"); system(pause); exit(0); } // 释放目标内存 if (T.ch) { delete[] T.ch; } // 初始化目标长度 T.length = S.length; // 分配内存 T.ch = new char[S.length + 1]; if (!T.ch) { printf("分配失败!"); system(pause); exit(-1); } // 复制字符 char *temps = S.ch; char *tempt = T.ch; while (*temps) { *tempt++ = *temps++; } *tent = '\0'; printf("成功!"); return true;} bool StrEmpty(S) { // 检查字符串是否存在 if (!S.ch) { printf("串不存在!"); system(pause); exit(0); } // 检查长度是否为0 if (!S.length) { printf("串为空!"); return true; } else { printf("串不为空!"); return false; }} int StrLength(S) { // 检查字符串是否存在 if (!S.ch) { printf("串不存在!"); system(pause); exit(0); } // 返回字符串长度 return S.length;} bool Contact(&T, S1, S2) { // 检查目标字符串是否存在 if (!T.ch) { printf("串不存在!"); system(pause); exit(0); } // 计算总长度 T.length = S1.length + S2.length; // 分配内存 T.ch = new char[T.length + 1]; if (!T.ch) { printf("分配失败!"); system(pause); exit(-1); } // 复制源字符 char *temp = T.ch; while (*S1.ch) { *temp++ = *S1.ch++; } while (*S2.ch) { *temp++ = *S2.ch++; } *temp = '\0'; printf("连接成功!"); return true;} bool SubString(&Sub, S, pos, len) { // 检查源字符串是否存在 if (!S.ch) { printf("串不存在!"); system(pause); exit(0); } // 检查参数合法性 if (pos < 1 || pos > S.length || len < 0 || len > S.length - pos + 1) { printf("Pos和len错误!"); Sub.ch = new char[1]; *Sub.ch = '\0'; Sub.length = 0; return false; } // 分配新内存 Sub.ch = new char[len + 1]; char *temp = Sub.ch; // 移动指针到起始位置 for (int i = 1; i != pos; ++i) { S.ch++; } // 复制字符 for (int i = 0; i != len; ++i) { Sub.ch++ = S.ch++; } return true;} bool Index(S, T, pos) { // 检查源和目标字符串是否存在 if (!S.ch || !T.ch) { printf("串不存在!"); system(pause); exit(0); } // 检查起始位置合法性 if (pos <= 1 || pos > S.length) { printf("Pos错误!"); return false; } // 初始化指针 char *tempt = T.ch; char *temps = S.ch; for (int i = 0; i < pos - 1; ++i) { ++tempt; ++temps; } // 比较字符 while (*tempt && *temps) { if (*tempt == *temps) { return true; } else { // 调整指针到匹配位置 ++tempt; --temps; if (tempt - temps > T.length) { return false; } } } return false;} bool StringReplace(&Str, T, V) { // 检查源、目标和替换字符串是否存在 if (!Str.ch || !T.ch || !V.ch || T.length == 0) { printf("串不存在或为空!"); system(pause); exit(0); } // 查找替换位置 int pos = Index(Str, T, 1); while (pos) { // 计算新长度 int nlength = Str.length + V.length - T.length; // 分配新内存 char *ctemp = new char[nlength + 1]; if (!ctemp) { printf("函数执行失败!"); system(pause); exit(-1); } char *temp = ctemp; char *stemp = Str.ch; char *vtemp = V.ch; // 复制前缀 for (int i = 0; i != pos; ++i) { *temp++ = *stemp++; } // 替换中间部分 for (int i = 0; i != T.length; ++i) { stemp++; } for (int i = 0; i != V.length; ++i) { *temp++ = *vtemp++; } // 处理后缀 while (*stemp) { *temp++ = *stemp++; } *temp = '\0'; // 更新目标字符串 delete Str.ch; Str.length = nlength; Str.ch = temp; // 继续查找下一个替换位置 pos = Index(Str, T, pos + V.length); } printf("子串替代成功!"); return true;} \0,长度为0,但分配了内存空间。转载地址:http://ulsh.baihongyu.com/