Implement MY_STRCHR () and MY_STRRCHR () in C language

Source: Internet
Author: User

The MY_STRCHR () function finds the position of the first occurrence of the character ch in the string str, and the prototype is as follows

Char *strchr (char const *str,int CH);

The MY_STRRCHR () function finds the last occurrence of a character in the string str, the prototype is as follows

Char *strrchr (char const *str,int CH);

#include <stdio.h>int main () {Char const * MY_STRCHR (char  const *STR, char ch);    Char Const * MY_STRRCHR (char  const *STR, char ch), char str[]= "ASFDGDHJJK", Char ch= ' d ';p rintf ("%s\n", MY_STRCHR (str, CH));p rintf ("%s\n", MY_STRRCHR (Str,ch)); return 0;} Char Const * MY_STRCHR (char  const *STR, char ch) {while (*str! = ') '//Determine if STR has found the last character {if (ch = = *str)// When you first find ch in str, return to Ch{return str;} str++;} Return null;//returns Null}char const * MY_STRRCHR (char  const *STR, char ch) {Char const*last = Null;char const*p when no ch is in Str Art = MY_STRCHR (str,ch);//Find where CH first appears in STR while (part! = NULL) {last = part;//assigns the position of ch to Lastpart = MY_STRCHR (part+1,ch) ;//Continue to determine whether there are ch}return last;//in STR and finally return to the last position of CH}


Implement MY_STRCHR () and MY_STRRCHR () in C language

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.