Home > Backend Development > C++ > What is the string search function in C language?

What is the string search function in C language?

WBOY
Release: 2023-08-28 13:05:05
forward
722 people have browsed it

What is the string search function in C language?

The library also provides several string search functions, as follows-

char *strchr (const char *string, intc);

#Find the first occurrence of character c in the string.

char "strrchr (const char "string, intc);

Find the last occurrence in the string character c.

char *strpbrk (const char *s1,const char *s2);

Returns a pointer pointing to A null pointer is returned on the first occurrence of any character in string s2 in string s1, or if the character in s2 does not exist in s1.

size_t strspn (const char *s1, const char *s2);

Return the beginning of s1 to match s2 number of characters.

size_t strcspn (const char *51, const char *s2);

Return the beginning of s1 Does not match the number of characters in s2.

char *strtok(c​​har *s1,const char *s2);

Disconnect pointer The string to converts si into a sequence of tokens, each token separated by one or more characters from the string pointed to by s2.

char * strtok_r(char *s1,const char *s2, char

has the same function as strtok() , except **lasts); The pointer to the string placeholder must be provided by the caller.

strchr () and strrchr () are the simplest to use.

Example 1

The following is the C program of String search function-

Real-time demonstration

#include <string.h>
#include <stdio.h>
void main(){
   char *str1 = "Hello";
   char *ans;
   ans = strchr (str1,&#39;l&#39;);
   printf("%s</p><p>", ans);
}
Copy after login

Output

When the above program is executed, the following results are produced -

llo
Copy after login

After executing this operation, ans points to location str1 2.

strpbrk () is a more general function that searches for the first occurrence of any group

Example 2

Following is the C program using strpbrk () function -

Live Demo

#include <string.h>
#include <stdio.h>
void main(){
   char *str1 = "Hello";
   char *ans;
   ans = strpbrk (str1,"aeiou");
   printf("%s</p><p>",ans);
}
Copy after login

Output

When the above program is executed, the following results are produced -

ello
Copy after login

Here, ans points to position str1 1, which is the position of the first e.

The above is the detailed content of What is the string search function in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
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