Implement strchr functions instead of using

Source: Internet
Author: User

When I was writing a program, I suddenly needed to locate a character in a string for the first time. So I found the strchr () function, which was never used before, ^ _ ^ of course, I can call it directly, but the laundry bag with good programming quality decides that it is not that difficult to implement this function. Loop traversal, until the first occurrence of the character you need to find ends. Hey, record it a little for future use.

Therefore, with a learning attitude, the following code emerged:


Char * mystrchr (const char * s, int C)// Return the address of the character you are looking

{

For (; * s! = (Char) C; ++ S)

{

If (* s = '\ 0 ')// The description is complete.

Return NULL;// If no result is found, the system returns NULL.

}

Return (char *) S;// Address of the character we need

}

We can define a string first.Then, we can use our own mystrchr () function,

For example, search for 'G ':

Puts (mystrchr (string address, 'G'); because it is defined in the int type, our characters are searched in the form of aiiss, the output is the string starting with the first G.


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.