C language STRCHR () function: Find where a character first appears in a string

Source: Internet
Author: User

Header files: #include <string.h>

STRCHR () is used to find the first occurrence of a character in a string, and its prototype is:
char * STRCHR (const char *STR, int c);

"Parameter" STR is the string to find, and C is the character to look for.

STRCHR () will find the address of the first occurrence of the character C in the STR string and return the address.

Note: The end flag of the string str NUL will also be included in the search scope, so a character from the STR group can also be positioned.

Return value returns the address of the character if the specified character is found, otherwise NULL is returned.

The address returned is the randomly assigned address of the string in memory plus the character you are searching for in the string position. The first occurrence of the set character in the string is I, then the returned address can be interpreted as STR + i.

Tip: If you want to find the last occurrence of a character in a string, you can use the STRRCHR () function.

"Instance" looks for the first occurrence of character 5.

Plain Text New window
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main(){
  5. char *s = "0123456789012345678901234567890";
  6. Char *p;
  7. P = strchr(S, ' 5 ');
  8. printf("%ld\ n", S);
  9. printf("%ld\ n", p);
  10. System("pause");
  11. return 0;
  12. }

Output Result:
12016464
12016469

C language STRCHR () function: Find where a character first appears in a string

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.