
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strspn() Function in C language?
The C library function size_t strspn(const char *str1, const char *str2) calculates the length of the initial segment of str1 which consists entirely of characters in str2.
An array of characters is called a string.
Declaration
Following is the declaration for an array −
char stringname [size];
For example − char string[50]; string of length 50 characters
Initialization
- Using single character constant −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char string[10] = "Hello":;
Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.
The Strspn() function
This function search for specified string in the given string and returns the no − of the char that are matched in the given string.
Declaration
Following is the declaration for the strspn() function −
size_t strspn(const char *string1, const char *string2)
Here,
string1 refers to the char of this string is search in string2.
string2 refers to an another string, the characters of this string are searched in string1.
Return value of strspn()
Returns no of characters that are matched in given string.
Example
The following example shows the usage of strspn() function.
#include <stdio.h> #include <string.h> int main (){ int length; char string1[20]; char string2[20]; printf("enter string1:
"); gets(string1); printf("enter string2:
"); gets(string2); /* Searching the string string2 in the string string1. * It returns the count of characters of string2 that * are matched in the string1 */ length = strspn(string1, string2); printf("The matched char are: %d
", length ); return 0; }
Output
When the above program is executed, it produces the following result −
enter string1: Tutorials enter string2: Tutorials Point The matched char are: 9
- Related Articles
- strspn() function in PHP
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok() function in C language?
- What is strtok_r() function in C language?
