博客
关于我
码图:156 扩展String类(C++)
阅读量:378 次
发布时间:2019-03-05

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

扩展String类,引入头文件CString.h,它的内容如下:

#include 
using namespace std;class String {protected: char *mystr; int len;public: String(const char *p) { len = strlen(p); mystr = new char[len + 1]; strcpy(mystr, p); } ~String() { if (mystr != NULL) { delete[] mystr; mystr = NULL; len = 0; } } void showStr() { cout << mystr << endl; }};

实现int IsSubString(int start, const char *str):

int EditString::IsSubString(int start, const char *str) {    int i, j;    for (i = 0; i < len; i++) {        int k = start + i;        for (j = 0; str[j] != '\0'; j++, k++) {            if (str[j] != mystr[k]) break;        }        if (str[j] == '\0') return start + i;    }    return -1;}

实现void EditChar(char s, char d):

void EditString::EditChar(char s, char d) {    int i = 0;    while (mystr[i]) {        if (mystr[i] == s) {            mystr[i] = d;        }        i++;    }}

实现void EditSub(char * subs, char * subd):

void EditString::EditSub(const char *subs, const char *subd) {    int i, j, len_s = strlen(subs), len_d = strlen(subd);    for (i = 0; i < len; i++) {        int k = i;        for (j = 0; subs[j] != '\0'; j++, k++) {            if (subs[j] != mystr[k]) break;        }        if (subs[j] == '\0') {            int m = i + len_s;            while (mystr[m - 1]) {                mystr[m + len_d - len_s] = mystr[m];                m++;            }            m = 0;            while (subd[m]) {                mystr[i + m] = subd[m];                m++;            }            i += (len_d - 1);        }    }}

实现void DeleteChar(char ch):

void EditString::DeleteChar(char ch) {    int i = 0;    while (mystr[i]) {        while (mystr[i] == ch) {            int j = i;            while (mystr[j]) {                mystr[j] = mystr[j + 1];                j++;            }        }        i++;    }}

实现void DeleteSub(const char *sub):

void EditString::DeleteSub(const char *sub) {    int i, j;    for (i = 0; i < len; i++) {        int k = i;        for (j = 0; sub[j] != '\0'; j++, k++) {            if (sub[j] != mystr[k]) break;        }        if (sub[j] == '\0') {            int len = strlen(sub);            int m = i + len;            while (mystr[m - 1]) {                mystr[m - len] = mystr[m];                m++;            }        }    }}

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

你可能感兴趣的文章
Python函数之返回值、作用域和局部变量
查看>>
Python+统计学 | 探索常用的数据分析统计分布
查看>>
python, selenium中用于切换帧的函数
查看>>
python函数
查看>>
python----线程、进程、协程的区别及多线程详解
查看>>
python---5
查看>>
python---使用celery分布式系统异步发送短信(云通讯)来验证登录
查看>>
python--003--百分号字符串拼接、format
查看>>
Python--10、线程
查看>>
Python--Redis实战:第四章:数据安全与性能保障:第4节:复制
查看>>
python--基本代码规范
查看>>
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>