Implementation of the STRCHR () function and the STRRCHR () function

Source: Internet
Author: User

Char *strchr (const char *s, int c)
Function: Find the position of the first C character in the string s

Description: Returns a pointer to the position of the first occurrence of C, the returned address is the first pointer to the string that is searched for the same character as C, and returns null if there is no C in s ....

Return value: Successfully returns the position of the first occurrence of the character to be found, otherwise returns null ....

The following is the STRCHR () function that you implement:

Char *my_strchr (const char *s, int c)
{
    if (s = = null)
    {
        return null;
    }

    while (*s! = ')
    } {
        if (*s = = (char) c)
        {
            return (char *) s;
        }
        s++;
    }
    return NULL;
}

Char *strrchr (const char *s, int c)

Function: Find a character C in the last occurrence of a string s (that is, starting from the right of S to find the first occurrence of the character C), and return all characters from the position where the character C in the string starts until the string s ends. If the character C is not found, NULL is returned.

The following is the function that you implement:

Char *strrchr (const char *s, int c)
{
    if (s = = null)
    {
        return null;
    }

    char *p_char = NULL;
    while (*s! = ')
    } {
        if (*s = = (char) c)
        {
            P_char = (char *) s;
        }
        s++;
    }

    return P_char;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.