How to find specified characters in a string in C language

coldplay.xixi
Release: 2023-01-03 09:30:14
Original
25506 people have browsed it

C language method to find specified characters in a string: 1. [strchr()] is used to find the position where a certain character first appears in a string; 2. [strrchr()] function is used to find a certain character The position of the last occurrence in the string.

How to find specified characters in a string in C language

The operating environment of this tutorial: windows7 system, c99 version, DELL G3 computer.

C language method of finding specified characters in a string:

1. strchr() is used to find the position where a character first appears in a string. Its prototype is:

    char * strchr (const char *str, int c);
Copy after login

[Parameter] str is the string to be found, and c is the character to be found.

strchr() will find the address of the first occurrence of character c in the str string, and then return the address.

Note: The end flag NUL of the string str will also be included in the search range, so the next character after the group of str can also be located.

[Return value] If the specified character is found, return the address of the character, otherwise return NULL.

The returned address is the randomly allocated address of the string in memory plus the position of the character you are searching for in the string. Assume the position where the character first appears in the string is i, then the returned address can be understood as str i.

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

[Example] Find the position where character 5 first appears.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char *s = "0123456789012345678901234567890";
    char *p;
    p = strchr(s, &#39;5&#39;);
printf("%ld\n", s);
    printf("%ld\n", p);
system("pause");
    return 0;
}
Copy after login

Output result:

12016464
12016469
Copy after login

2. The strrchr() function is used to find the last occurrence of a character in a string. Its prototype is:

    char * strrchr(const char *str, int c);
Copy after login

[Parameter] str is the string to be found, and c is the character to be found.

strrchr() will find the address of the last occurrence of character c in the str string and return that address.

Note: The end flag NUL of the string str will also be included in the search range, so the next character after the group of str can also be located.

[Return value] If found, return the position where the character last appeared, otherwise return NULL.

The returned address is the randomly allocated address of the string in memory plus the position of the character you are searching for in the string. Assume the position where the character first appears in the string is i, then the returned address can be understood as str i.

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

Example: Find the last occurrence of character 5.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char *s = "0123456789012345678901234567890";
    char *p;
    p = strrchr(s, &#39;5&#39;);
    printf("%ld\n", s);
    printf("%ld\n", p);
    system("pause");
    return 0;
}
Copy after login

Execution result:

12999504
12999529
Copy after login

[Related learning recommendations: C language tutorial video]

The above is the detailed content of How to find specified characters in a string in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!