pointer on C programming exercise P6.18 Q1

来源:互联网 发布:淘宝买东西怎样付款 编辑:程序博客网 时间:2024/05/24 23:12
/****************************************file name:****************************************//****************************************include files****************************************/#include "stdio.h"/****************************************function declaration****************************************/char *find_char(char const *source, char const *chars);/****************************************function realization****************************************/int main(void){    char *pDst = NULL;    char *ppStr[] =     {        "ABCDEF",  // 0        "XYZ",     // 1        "JURY",    // 2        "QQQQ",    // 3        "XRCQEF",  // 4        "",        // 5        NULL       // 6    };        //abnormal test    pDst = find_char(NULL, NULL);    pDst = find_char(NULL, ppStr[0]);    pDst = find_char(ppStr[0], NULL);    pDst = find_char(ppStr[5], ppStr[0]);    pDst = find_char(ppStr[0], ppStr[5]);    pDst = find_char(ppStr[6], ppStr[0]);    pDst = find_char(ppStr[0], ppStr[6]);        //normal test    pDst = find_char(ppStr[0], ppStr[1]);    pDst = find_char(ppStr[0], ppStr[2]);    pDst = find_char(ppStr[0], ppStr[3]);    pDst = find_char(ppStr[0], ppStr[4]);    if(NULL != pDst)    {        printf("Got the position %d and char %c in %s\n", pDst-ppStr[0], *pDst, ppStr[0]);    }        pDst = find_char(ppStr[0], ppStr[5]);    pDst++;    return 0;}/****************************************name: find_char*function: find chars in strings and return char position****************************************/char *find_char(char const *pSource, char const *pChars){    char *pSrcloc = NULL;        //check parameter    if((NULL == pSource) || (NULL == pChars) || (NULL == *pSource) || (NULL == *pChars))    {        return NULL;    }        //ourter loops tracks destination string    while(*pChars)    {        //inter loops tracks source string        pSrcloc = (char *)pSource;        while(*pSrcloc)        {            if((*pSrcloc) == (*pChars))            {                //find the posotion                return pSrcloc;            }            else            {                pSrcloc++;            }        }                pChars++;    }        return NULL;}


0 0