Header file:
string.h
Synopsis:
size_t strspn(char *str, char *search_str);
Description:
It returns the length of the prefix of str which consists of characters in string search_str.
strspn function C example:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main () {
char input1[100];
int ret;
printf("Enter a string:");
fgets(input1, 90, stdin);
ret = strspn(input1, "abcdefghijklnmopqrstuvwxyz");
printf("Return value: %d\n", ret);
return 0;
}
#include<string.h>
#include<stdlib.h>
int main () {
char input1[100];
int ret;
printf("Enter a string:");
fgets(input1, 90, stdin);
ret = strspn(input1, "abcdefghijklnmopqrstuvwxyz");
printf("Return value: %d\n", ret);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter a string:hello world
Return value: 5
Enter a string:hello world
Return value: 5
Here, "hello world" is the input string. And we don't have space in search_str ("abcdefghijklmnopqrstuvwxyz"). So, the number of characters in input string("hello world") that are present before space is returned as return value. In our case, there are 5 characters before space("hello") and the same value is returned as return value.
No comments:
Post a Comment