zoukankan      html  css  js  c++  java
  • 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

    1.strcpy字符串拷贝
    拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外)

    复制代码
    char *strcpy(char *pStrDest, const char *pStrSource)
    {
        assert(NULL!=pStrDest && NULL!=pStrSource);
        char *strTemp=pStrDest;
        while ((*pStrDest++ = *pStrSource++) != '');
    
        return strTemp;
    }
    复制代码

    2.strcmp字符串比较

    复制代码
    int strcmp(const char *pStrA, const char *pStrB)
    {
        assert(NULL!=pStrA && NULL!=pStrB);
        while (*pStrA && *pStrB && *pStrA==*pStrB)
        {
            ++pStrA;
            ++pStrB;
        }
    
        return (pStrA-*pStrB);
    }
    复制代码

    3.strstr字符串查找

    复制代码
    char *strstr(const char *pStrSource, const char *pStrSearch)   
    {   
        assert(pStrSource != NULL && pStrSearch != NULL); 
        const char *strTempSource = pStrSource;
        const char *strTempSearch = pStrSearch;
        for (; *pStrSource!=''; ++pStrSource)
        {
            for (strTempSource=pStrSource,strTempSearch=pStrSearch;
            *strTempSearch!='' && *strTempSearch==*strTempSource;
            ++strTempSource, ++strTempSearch);
    
            if (*strTempSearch == '')
            {
                return (char *)pStrSource;
            }
        }
    
        return (char *)NULL; 
    } 
    复制代码

    4.strDelChar字符串删除字符

    复制代码
    char *strDelChar(char *pStrSource, const char chDel)
    {
        assert(NULL!=pStrSource && !isspace(chDel));
        char *pTempStrA, *pTempStrB;
        pTempStrA = pTempStrB = pStrSource;
    
        while (*pTempStrB++)
        {
            if (*pTempStrB != chDel)
            {
                *pTempStrA++ = *pTempStrB; 
            }
        }
        *pTempStrA = '';
    
        return pStrSource;
    }
    复制代码

    5.strrev字符串反序

    复制代码
    char *strrev(char *pStrSource)
    {
        assert (NULL != pStrSource);
    
        char *pStrStart, *pStrEnd;
        pStrStart = pStrEnd = pStrSource;
        while (*pStrEnd != '')
        {
            ++pStrEnd;
        }
    
        char chTemp;
        for (--pStrEnd, pStrStart; pStrEnd<pStrStart; ++pStrStart, --pStrEnd)
        {
            chTemp = *pStrStart;
            *pStrStart = *pStrEnd;
            *pStrEnd = chTemp;
        }
    
        return pStrSource;
    }
    复制代码

    6.memmove拷贝内存块

    复制代码
    void *memmove(void *pStrTo, const void *pStrFrom, size_t count)
    {
        assert (NULL!=pStrTo && NULL!=pStrFrom);
    
        void *pStrRet = pStrTo;
    
        if (pStrTo<pStrFrom || pStrTo>pStrFrom+count-1)
        {
            //内存块不重叠情况
            while (count--)
            {
                *pStrTo++ = *pStrFrom++;
            }
        }
        else
        {
            //内存块重叠情况
            char *pStrDest = (char *)pStrTo;
            char *pStrSource = (char *)pStrFrom;
            pStrDest = pStrDest+count-1;
            pStrSource = pStrSource+count-1;
            while (count--)
            {
                *pStrDest-- = *pStrSource--;
            }
        }
    
        return pStrRet;
    }
    复制代码

    7.strlen字符串长度

    复制代码
    int strlen(const char *pStrSource)
    {
        assert(NULL != pStrSource);
        int iLen = 0;
        while (*pStrSource++ != '')
        {
            ++iLen;
        }
    
        return iLen;
    }
    复制代码

    http://www.cnblogs.com/sz-leez/p/4531507.html

  • 相关阅读:
    深入探讨 Python 的 import 机制:实现远程导入模块
    PEP8中文版 -- Python编码风格指南
    【转】python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法>
    【转】Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
    Python字典的json格式化处理(换行与不换行)
    最详细的CentOS 6与7对比(三):性能测试对比
    最详细的CentOS 6与7对比(二):服务管理对比
    WEBAPI 自动生成帮助文档
    MEF IOC使用
    MVC分页示例
  • 原文地址:https://www.cnblogs.com/findumars/p/8824143.html
Copyright © 2011-2022 走看看