《C和指针》第六章编程练习第一题

思路:让source中的每一个字符遍历一遍chars中的字符,在遍历过程中若两个字符相等,则中断遍历并返回source中该字符的地址,否则返回NULL。

char *find_char( char const *source, char const *chars)
{
    /* 定义临时变量 */
    char * tmp;
    /* 判断指针是否异常 */
    if ( source != NULL && chars != NULL)
    {
        /* 循环查找 */
        for (; *source != '\0'; source++ )
        {
            for (tmp = chars; *tmp != '\0'; tmp++)
            {
                if ( *source == *tmp )
                    return source;
            }
        }
    }
    return NULL;
}

猜你喜欢

转载自blog.csdn.net/weixin_41354745/article/details/82585758