不使用庫函數,實現字符串操作源碼

/******************************************************************************
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;
}
發佈了78 篇原創文章 · 獲贊 54 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章