博客
关于我
串的基本操作实现源码|数据结构
阅读量:333 次
发布时间: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/

你可能感兴趣的文章
OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll(转))
查看>>
opengl绘制几何体的函数
查看>>
openGL缓存概念和缓存清除(01)
查看>>
OpenJDK11 下的HSDB工具使用入门
查看>>
openjdk踩坑
查看>>
openjudge 1792 迷宫 解析报告
查看>>
OpenJudge/Poj 1658 Eva's Problem
查看>>
Openlayers 9.0新功能
查看>>
Openlayers Draw的用法、属性、方法、事件介绍
查看>>
Openlayers layer 基础及重点内容讲解
查看>>
Openlayers map三要素(view,target,layers),及其他参数属性方法介绍
查看>>
Openlayers Map事件基础及重点内容讲解
查看>>
Openlayers Select的用法、属性、方法、事件介绍
查看>>
Openlayers Source基础及重点内容讲解
查看>>
Openlayers view三要素(zoom,center,projection)及其他参数属性方法介绍
查看>>
OpenLayers 入门使用
查看>>
Openlayers 入门教程(一):应该如何学习 Openlayers
查看>>
openlayers 入门教程(七):Interactions 篇
查看>>
openlayers 入门教程(三):view 篇
查看>>
openlayers 入门教程(九):overlay 篇
查看>>