pointer on C programming exercise P6.18 Q2

来源:互联网 发布:胡夫金字塔的结构数据 编辑:程序博客网 时间:2024/05/22 17:45
/****************************************file name:****************************************//****************************************include files****************************************/#include "stdio.h"/****************************************function declaration****************************************/int delete_substr(char *source, char const *chars);/****************************************function realization****************************************/int main(void){    int  iReturn  = 0;    char *ppStr[] =     {        "ABCDEFG", // 0        "FGH",     // 1        "CDF",     // 2        "XABC",    // 3        "CDE",     // 4        NULL       // 5    };        //abnormal test    iReturn = delete_substr(NULL, NULL);    iReturn = delete_substr(NULL, ppStr[0]);    iReturn = delete_substr(ppStr[0], NULL);    iReturn = delete_substr(ppStr[5], ppStr[0]);    iReturn = delete_substr(ppStr[0], ppStr[5]);        //normal test    iReturn = delete_substr(ppStr[0], ppStr[1]);    iReturn = delete_substr(ppStr[0], ppStr[2]);    iReturn = delete_substr(ppStr[0], ppStr[3]);    iReturn = delete_substr(ppStr[0], ppStr[4]);    if(1 == iReturn)    {        printf("delete success: %s, substr %s\n", ppStr[0], ppStr[4]);    }        return 0;}/****************************************name: delete_substr*function: delete sub string if found****************************************/int delete_substr(char *pSource, char const *pChars){    char *pSrcloc = NULL;  // pointer to the source string in inter loops    char *pSubloc = NULL;  // pointer to the destination string in inter loops    char *pSrcstr = NULL;  // pointer to the source string in outer loops    int   bFind   = 0;     // flag indicates if find the substring.        //check parameter    if((NULL == pSource) || (NULL == pChars) || (NULL == *pSource) || (NULL == *pChars))    {        return 0;    }        //inter loops tracks source string    pSrcstr = pSource;    while(*pSrcstr)    {        if((*pSrcstr) == (*pChars))        {            //find the matched char position            pSrcloc = (char *)pSrcstr;            pSubloc = (char *)pChars;            while(*pSubloc)            {                if(*pSubloc != *pSrcloc)                {                    //search end, doesn't match                    bFind = 1;                    break;                }                else                {                    //match the next char                    pSubloc++;                    pSrcloc++;                }            }                        //check if find            if(0 == bFind)            {                //delete the substring                pSubloc = (char *)pChars;                while(*pSubloc)                {                    *pSrcstr = *pSrcloc;                    pSrcstr++;                    pSubloc++;                    pSrcloc++;                }                                //clear the tail of source string                while(*pSrcstr)                {                    *pSrcstr = '\0';                    pSrcstr++;                }                                return 1;            }            else            {                // substr fait to match                pSrcstr++;            }        }        else        {            pSrcstr++;        }    }        return 0;}

0 0
原创粉丝点击