C++实现字符串相关操作

jackxiang 2010-5-15 00:16 | |

#include "assert.h"


//计算字符串src的长度
int strlen(const char* str)
{
    int i = 0;
    while(*(str+i) != ''\0'')
    {
        i++;
    }
    return i;
}

//将字符串src copy 到 des中
char* strcpy(char* des, const char* src)
{
    assert((des != NULL) && (src != NULL));//确认des和src均不是空指针

    char* desAddress = des;

    //copy
    while ((*des++ = *src++) != ''\0'')
    {
        NULL;
    }

    return desAddress;
}

//将src连接到des后面
char* strcat(char* des, const char* src)
{

    char* desAddress = des;

    //des指针移动到最后
    while (*des++ != ''\0'')
    {
        NULL;
    }

    //指针后退一位,指向''\0''的位置
    des--;

    //concat
    while ((*des++ = *src++) != ''\0'')
    {
        NULL;
    }

    return desAddress;
}

//截取src的第s到e个字符,将得到的子串返回
char* SubString(char* des, const char* src, int s, int e)
{
    assert((des != NULL) && (src != NULL));

    char* desAddress = des;

    //如果e的值超出字符串的长度,则截取从s到字符串最后的字符
    int maxlength = (int)strlen(src);
    if (e > maxlength)
    {
        e = maxlength;
    }

    //得到从e开始到s的长度为e-s的字符串
    int tk = 0;
    while (tk <= e-s)
    {
        *(des+tk) = *(src+tk+s);
        tk++;
    }

    //结束字符串
    *(des+tk) = ''\0'';

    return desAddress;
}

//将src插入到des的第p个字符之后
char* StringInsert(char* des, const char* src, int p)
{
    assert((src != NULL) && (des != NULL) && (p >= 0));
    
    char* desAddress = des;

    //如果位置p超出字符串的最大长度,则直接将src连接到des后面
    if (p >= (int)strlen(des))
    {
        strcat(des,src);
        return desAddress;
    }
    
    int srcLength = (int)strlen(src);

    //存储第p个字符后面的字符串,存储在tempps中
    char* tempps = new char[strlen(des)-p+1];
    strcpy(tempps,des+p);

    //指针pps指向第p+1个字符
    char* pps = des+p;

    //将src copy到第p个字符后面
    while ((*pps++ = *src++) != ''\0'')
    {
        NULL;
    }

    //指针前移一位,指向字符串结束符''\0''
    pps--;
    
    //循环,将tempps copy 到pps后面,此时pps指向des+p+srcLength-1
    while ((*pps++ = *tempps++) != ''\0'')
    {
        NULL;
    }
    //pps--;
    //strcat(pps,tempps);

    return desAddress;
}

//交换两个字符
void swap(char& sc, char& dc)
{
    char temp = sc;
    sc = dc;
    dc = temp;
}

//交换字符串str中的第i,j个字符,以字符串数组返回
char* swapchars(const char* str, const int i, const int j)
{
    assert(str != NULL);
    char* dstr = new char[strlen(str)+1];
    strcpy(dstr,str);
    swap(*(dstr+i),*(dstr+j));

    return dstr;
}

//倒置字符串,例如将"abcdef"变为"fedcba"
char* strrev(char* str)
{
    assert(str != NULL);
    int len = (int)strlen(str);
    char* p = str;
    char* data = new char[len+1];
    //char* q = data;
    //while (*p++ != ''\0'')
    //{
    //    NULL;
    //}
    //p--;
    //while((*data++ = *p--) != NULL)
    //{
    //    NULL;
    //}
    //*(data+len) = ''\0'';
    for (int i = len-1; i>=0; i--)
    {
        *(data+i) = *p++;
    }
    *(data+len) = ''\0'';

    return data;
}


//将字符串按照单词进行倒序,例如将"just do it"变为"it do just"
char* wordrev(char* str)
{
    assert(str != NULL);
    char* p = str;
    int len = (int)strlen(str);
    int s = 0;
    int e = len;
    int k = 0;
    
    char* data = new char[len+1];

    for (int i = len-1; i >= 0; i--)
    {
        /*if ((str[i] != '' '') && (i != 0)) continue;
        else
        {
            s = i + 1;
            if (i == 0)
            {
                s = i;
            }
            for (int j = s; j < e; j++)
            {
                data[k++] = str[j];
            }
            data[k++] = '' '';
            e = i;
        }*/
        if ((*(str+i) != '' '') && (i != 0)) continue;
        else
        {//找到空格
            s = i + 1;
            if (i == 0)//如果处理到第一个字符
            {
                s = i;
            }
            //将获得的单词(源串中从第s到第e个字符)
            for (int j = s; j < e; j++)
            {
                *(data+k++) = *(str+j);
            }
            *(data+k++) = '' '';//补齐空格
            e = i;
        }
    }
    *(data+k) = ''\0'';
    return data;
}

//从一个字符串中删除另一个字符串中出现的字符
char* delchars(char* str, const char* src)
{
    assert((str != NULL) && (src != NULL));

    int len = (int)strlen(str);
    char* data = new char[len+1];
    char* p = data;

    //对原数据进行一个copy,防止源数据为只读
    while ((*data++ = *str++) != ''\0'')
    {
        NULL;
    }

    char* res = p;
    int srclen = (int)strlen(src);

    while (*res != ''\0'')//对源串进行循环
    {
        for (int i = 0; i < srclen; i++)//依次与src中的每个字符进行比较
        {
            if (*res == *(src+i))//如果当前字符与src中的某个字符相等
            {
                int i = 0;
                while (*(res+i) != ''\0'')//将当前字符后面的所有字符前移一位
                {
                    *(res+i) = *(res+i+1);
                    i++;
                }
            }
        }
        res++;
    }
    return p;
}

//输出字符串中第一个不重复字符
char firstnorepeat(const char* str)
{
    assert(str != NULL);

    int len = (int)strlen(str);
    char* ch = new char[len+1]; //"有重复字符"数组
    char* q = ch;
    int k = 0;

    bool flag = true; //是否是"第一个不重复字符"的标记

    for (int i = 0; i < len-1; i++) //对str进行循环
    {
        flag = true;//默认当前字符为"第一个不重复的字符"
        
        //判断当前字符是否已经存在于"有重复字符"数组中
        for (int m = 0;m < (int)strlen(ch); m++)
        {
            if (*(str+i) == ch[m])
            {
                flag = false; //如果当前字符存在于"有重复字符"数组中,则标记为false
            }
        }

        //如果当前字符不再"有重复字符"数组中,则与当前字符后面的每个字符进行比较
        for(int j = i+1; j < len && flag; j++)
        {
            if (*(str+i) == *(str+j))    //如果当前字符与其后面的某个字符相等,则这个字符不是第一个不重复字符
        &nbsp;   {
                flag = false;    //则这个字符不是第一个不重复字符
                ch[k++] = *(str+i);    //将这个字符添加到"有重复字符"数组中
            }
        }

        if(flag)    //如果当前字符既不在"有重复字符"数组中,也不与其后面的任何一个字符相等
        {
            return *(str+i);    //当前字符就是"第一个不重复字符"
        }
    }

        return ''\0'';    //如果所有的字符都有重复,返回''\0''
}

//判读一个字符是否在一个字符串中,如果在,返回字符在字符串中首次出现的下标,否则返回-1
int IsExistInString(const char* str,char c)
{
    assert(str != NULL);

    int i = 0;
    while (*(str+i) != ''\0'')
    {
        if (*(str+i) == c)
        {
            return i;
        }
        i++;
    }
    return -1;
}

//统计一个字符串str中所有字符的出现次数,
//data记录str中的所有不重复字符,
//num记录与data下标相对应的字符出现的次数
void StatisticChars(char* str, char* data, int* num)
{
    assert(str != NULL);    //确保字符串str不是空串

    int len = (int)strlen(str);

    //对记录次数的数组赋初值0
    for (int i = 0; i < len; i++)
    {
        num[i] = 0;
    }

    //将str中的第一个字符加入到data中
    data[0] = str[0];
    num[0] = 1;
    int k = 1;

    //从第一个字符开始搜索字符串
    for (int i = 1; i < len; i++)
    {
        //找出第i个字符在data中的位置
        int j = IsExistInString(data,str[i]);
        if (j < 0)    //如果第i个字符不在data中
        {
            num[k]++;
            data[k++] = str[i];
            data[k] = ''\0'';
        }
        else //如果第i个字符是data中下标为j的字符
        {
            num[j]++;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    
    //使用的例子说明
    char* ch = "abca defdb ghicefghiquikelaaaaa";
    char* dch = NULL;
    dch = new char[50];
    dch[0] = ''b'';
    dch[1] = ''h'';
    dch[2] = ''3'';
    dch[3] = ''\0'';

    //char* catc = SubString(dch,ch,1,3);
    //StringInsert(dch,ch,1);
    //printf("%s\n",dch);

    //char* catc = strcat(dch,ch);

    //printf("%s\n",catc);
    //char* cch = NULL;
    //cch = new char[100];
    //cch[0] = ''1'';
    //strcpy(cch,ch);
    //printf("%s\n",cch);
    //char* tc = strrev(ch);
    //printf("%s\n",tc);
    //char* tc = wordrev(ch);
    //printf("%s\n",tc);
    //char* dc = delchars(ch,dch);
    //printf("%s\n",dc);
    //char c = firstnorepeat(ch);
    //printf("%c\n",c);
    //int i = IsExistInString(ch,''a'');
    //printf("%d\n",i);
    //int len = (int)strlen(ch);
    //int* num = new int[len+1];
    //char* data = new char[len+1];
    //data[0] = ''\0'';
    //StatisticChars(ch,data,num);
    //for (int i = 0; i < (int)strlen(data); i++)
    //{
    //    printf("%c\t%d\n",data[i],num[i]);
    //}
    return 0;
}


来源:http://simawenxue.blog.hexun.com/34997542_d.html


作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/3072/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]