博客
关于我
串的基本操作实现源码|数据结构
阅读量:334 次
发布时间:2019-03-04

本文共 4497 字,大约阅读时间需要 14 分钟。

字符操作函数详细说明

结构定义

typedef struct {    char *ch;    int length;} HStirng;

字符串操作函数

1. 初始化字符串

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;    }}

2. 字符串复制

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;}

3. 检查空字符串

bool StrEmpty(S) {    // 检查字符串是否存在    if (!S.ch) {        printf("串不存在!");        system(pause);        exit(0);    }    // 检查长度是否为0    if (!S.length) {        printf("串为空!");        return true;    } else {        printf("串不为空!");        return false;    }}

4. 计算字符串长度

int StrLength(S) {    // 检查字符串是否存在    if (!S.ch) {        printf("串不存在!");        system(pause);        exit(0);    }    // 返回字符串长度    return S.length;}

5. 字符串连接

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;}

6. 取子字符串

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;}

7. 字符串索引查找

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;}

8. 字符串替换

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;}

注意事项

  • 清空字符串:ch为\0,长度为0,但分配了内存空间。
  • 字符串不存在:没有分配内存空间。

转载地址:http://ulsh.baihongyu.com/

你可能感兴趣的文章
Python基础(7)--函数
查看>>
Python基础部分-while循环,格式化输出
查看>>
Python基础语法:顺序语句结构
查看>>
Python基础语法:理解代码与写代码
查看>>
Python基础语法:条件和分支
查看>>
Python基础语法:基本数据类型(数字类型和布尔类型)
查看>>
Python基础语法:基本数据类型(列表)
查看>>
Python基础语法:内置函数
查看>>
Python基础语法:你不得不知的几种变量类型
查看>>
Python基础语法教程,零基础入门到精通,看完这一篇就够了
查看>>
Python基础语法与执行脚本的3种方式
查看>>
python基础语法
查看>>
python基础语法
查看>>
python系列【仅供参考】:python测试开发基础---multiprocessing.Pool
查看>>
Python基础综合练习
查看>>
Python基础练习之一输出10000以内的阿姆斯特朗数
查看>>
Python基础系列讲解—动态类型语言的特点
查看>>
python基础系列四:字符串,列表,字典,元组之间的转换
查看>>
python基础篇(9):模块
查看>>
python基础篇(8):异常处理
查看>>