C++ 中的 strpbrk()
这是一个 C++ 中的字符串函数,它需要两个字符串,在 string1 中找到 string2 中任何字符的第一个出现。如果 string1 中存在该字符,则返回指向该字符的指针,否则返回 NULL。对于终止 NULL 字符不适用。
strpbrk() 的语法如下 −
char *strpbrk(const char *str1, const char *str2)
在上面的语法中,strpbrk() 返回 str1 中与 str2 中任何字符匹配的第一个字符的指针。
演示 strpbrk() 的程序如下。
示例
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[20] = "aeroplane";
char str2[20] = "fun";
char *c;
c = strpbrk(str1, str2);
if (c != 0)
cout<<"First matching character in str1 is "<< *c <<" at position "<< c-str1+1;
else
printf("Character not found");
return 0;
}输出
First matching character in str1 is n at position 8
在上面的程序中,首先定义了两个字符串 str1 和 str2。strpbrk() 返回的指向 str1 中字符的指针存储在 c 中。如果 c 的值不为 0,则显示该字符及其在 str1 中的位置。否则,str1 中不存在该字符。以下代码片段对这种情况进行了演示。
char str1[20] = "aeroplane";
char str2[20] = "fun";
char *c;
c = strpbrk(str1, str2);
if (c != 0)
cout<<"First matching character in str1 is "<<*c <<" at position "<< c-str1+1;
else
printf("Character not found");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP