这个 strcspn() C/C++中的函数以两个字符串作为输入, 字符串_1 和 字符串2 作为参数,通过遍历字符串_2中存在的任何字符来搜索字符串_1。如果在字符串_1中找不到字符串_2的任何字符,函数将返回字符串_1的长度。此函数在中定义 cstring 头文件。 语法:
null
size_t strcspn ( const char *string_1, const char *string_2 )
参数: 该函数接受两个强制参数,如下所述:
- 字符串_1: 指定要搜索的字符串
- 字符串2: 指定包含要匹配的字符的字符串
返回值: 此函数返回在字符串_1中经过的字符数,在与字符串_2匹配的任何字符之前。 以下程序说明了上述功能: 项目1:
CPP
// C++ program to illustrate the // strcspn() function #include <bits/stdc++.h> using namespace std; int main() { // String to be traversed char string_1[] = "geekforgeeks456"; // Search these characters in string_1 char string_2[] = "123456789"; // function strcspn() traverse the string_1 // and search the characters of string_2 size_t match = strcspn (string_1, string_2); // if matched return the position number if (match < strlen (string_1)) cout << "The number of characters before" << "the matched character are " << match; else cout << string_1 << " didn't matched any character from string_2 "; return 0; } |
输出:
The number of characters beforethe matched character are 12
项目2:
CPP
// C++ program to illustrate the // strcspn() function When the string // containing the character to be // matched is empty #include <bits/stdc++.h> using namespace std; int main() { // String to be traversed char string_1[] = "geekforgeeks456"; // Search these characters in string_1 char string_2[] = ""; // Empty // function strcspn() traverse the string_1 // and search the characters of string_2 size_t match = strcspn (string_1, string_2); // if matched return the position number if (match < strlen (string_1)) cout << "The number of character before" << "the matched character are " << match; else cout << string_1 << " didn't matched any character from string_2 "; return 0; } |
输出:
geekforgeeks456 didn't matched any character from string_2
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END