Home >Backend Development >C#.Net Tutorial >How to find specified characters in a string in C language

How to find specified characters in a string in C language

coldplay.xixi
coldplay.xixiOriginal
2021-03-17 15:41:1325669browse

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);

[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;
}

Output result:

12016464
12016469

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);

[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;
}

Execution result:

12999504
12999529

[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!

Statement:
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