English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This functionstrcspn()
Calculates the number of characters before the first matching character in two strings. This is declared in the "string.h" header file. It returns the number of characters in the first string before the first matching character.
This isstrcspn()
C language syntax,
size_t strcspn(const char *string1, const char *string2)
Here,
string1-the first string to be scanned.
string2-the second string used to search for matching characters in the first string.
This isstrcspn()
C language example,
#include<stdio.h> #include<string.h> int main() { char str1[] = "Helloworld!"; char str2[] = "work"; int result = strcspn(str1, str2); printf("Number of characters before matching character: %d\n", (result+1)); return 0; }
Output result
Number of characters before matching character: 5
In the above program, two char type arrays are declared, and the string is passed to them. This functionstrcspn()
Calculating the number of characters before the first match of "wor". Therefore, the first string contains5characters do not match. Therefore, the output is5and store it in the variable result.
char str1[] = "Helloworld!"; char str2[] = "work"; int result = strcspn(str1, str2);