不使用库函数,实现字符串操作源码

/******************************************************************************
File Name     : 
Version       : 
Author        : 
Created       : 2014/01/16
Last Modified :
Description   : 
Function List :


History       :
1.Date        : 2014/01/16
Author      : 
Modification: Created file


******************************************************************************/
#include <stdlib.h>


unsigned int strlenth(char *s)  /* 获取字符串长度 */
{
    /* 代码在这里实现 */
    unsigned int lenth = 0;

    if (NULL == s && NULL == *s)
    {
        return 0;
    }
    while(*s != '\0')
    {
        lenth++;
        s++;
    }
    return lenth;
}

void strcopy(char **target, char *source)  /* 字符串拷贝 */
{
    /* 代码在这里实现 */
    int iStrLen         = 0;
    int iLoop            = 0;
    char * szTemp = NULL;

    if (NULL == source && NULL == *source)
    {
        return ;
    }
    iStrLen = strlenth(source);
    szTemp = (char *)malloc(iStrLen + 1);
    if (NULL == szTemp)
    {
        return;
    }
    for(iLoop = 0; iLoop < iStrLen; iLoop++)
    {
        szTemp[iLoop] = source[iLoop];
    }
    szTemp[iStrLen] = '\0';
    *target = szTemp;
    return;
}

int strcompare(char *s, char *t)  /* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
{
    /* 代码在这里实现 */
    int iResult         = 0;
    int iSrcStrLen   = 0;
    int iTagSrcLen = 0;
    int iLoop            = 0;

    iSrcStrLen   = strlenth(s);
    iTagSrcLen = strlenth(t);
    if ( iSrcStrLen > iTagSrcLen)
    {
        while(iTagSrcLen--)
        {
            if ((int)s[iLoop] > (int)t[iLoop])
            {
                return 1;
            }
            else
            {
                if ((int)s[iLoop] < (int)t[iLoop])
                {
                    return -1;
                }
                continue;
            }
            iLoop++;
        }
        if (iSrcStrLen > iTagSrcLen)
        {
            return 1;
        }
        else
            return -1;
    }
    else
    {
        if (iSrcStrLen < iTagSrcLen)
        {
            while(iSrcStrLen--)
            {
                if ((int)s[iLoop] > (int)t[iLoop])
                {
                    return 1;
                }
                else
                {
                    if ((int)s[iLoop] < (int)t[iLoop])
                    {
                        return -1;
                    }
                    continue;
                }
                iLoop++;
            }
            if (iSrcStrLen > iTagSrcLen)
            {
                return 1;
            }
            else
                return -1;
        }
        else
        {
            while(iSrcStrLen--)
            {
                if ((int)s[iLoop] > (int)t[iLoop])
                {
                    return 1;
                }
                else
                {
                    if ((int)s[iLoop] < (int)t[iLoop])
                    {
                        return -1;
                    }
                    continue;
                }
                iLoop++;
            }  
        }
    }
    return 0;
}

void strcombine(char **x, char *s, char *t)  /* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
{
    /* 代码在这里实现 */
    int iSrcStrLen  = 0;
    int iTagStrLen = 0;
    int iLoop           = 0;
    char *pTemp  = NULL;

    iSrcStrLen = strlenth(s);
    iTagStrLen = strlenth(t);
    pTemp = (char *)malloc(iSrcStrLen + iTagStrLen + 1);
    if (NULL == pTemp)
    {
        return ;
    }
    for (iLoop; iLoop < iSrcStrLen; iLoop++)
    {
        pTemp[iLoop] = s[iLoop];
    }
    for (iLoop = 0; iLoop < iTagStrLen; iLoop++)
    {
        pTemp[iSrcStrLen + iLoop] = t[iLoop];
    }
    pTemp[iSrcStrLen + iTagStrLen] = '\0';
    *x = pTemp;
    return ;
}

void strcatch(char *s, unsigned int index, unsigned int lenth, char **t)  /* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
{
    /* 代码在这里实现 */
    int iLoop                            = 0;
    unsigned int iSrcStrLen = 0;
    char *pTemp                    = NULL;

    iSrcStrLen = strlenth(s);
    if ((lenth > iSrcStrLen) || (lenth == 0) || ((index + lenth) > iSrcStrLen))
    {
        return ;
    }
    pTemp = (char *)malloc(iSrcStrLen + 1);
    if (NULL == pTemp)
    {
        return ;
    }
    while( lenth--)
    {
        pTemp[iLoop] = s[index];
        iLoop++;
        index++;
    }
    pTemp[iLoop] = '\0';
    *t = pTemp;
    return ;
}

bool strsubstr(char *s, char *sub)  /* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
{
    /* 代码在这里实现 */
    bool result       = 0;
    int iSrcStrLen  = 0;
    int iSubStrLen = 0;
    int iLoop           = 0;
    int jLoop           = 0;

    iSrcStrLen = strlenth(s);
    iSubStrLen = strlenth(sub);
    for (iLoop; iLoop < iSubStrLen; iLoop++)
    {
        for (jLoop; jLoop < iSrcStrLen; jLoop++)
        {
            if (sub[iLoop] == s[jLoop])
            {
                iLoop++;
                if ('\0' == sub[iLoop])
                {
                    result = 1;
                    break;
                }
                continue;
            }
        }
        iLoop++;
    }
    return result;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值