Javatpoint Logo
Javatpoint Logo

Strspn() Function in C++

String manipulation is an essential part of handling and processing textual data in the C and C++ computer languages. The C standard library offers a helpful method called strspn() that may be used to calculate the length of the first segment of a string that is made up only of characters from a certain set.

An overview of the function strspn()

The header files for or contain the strspn() function. It uses C-style strings and has the prototype described below:

size_t strspn(const char *strings, const char *character);

Str: An index of the string for a program.

chars: A pointer to the characters in str that need to be searched, contained in a null-terminated string.

Goal and Conduct:

The main goal of strspn() is to calculate the length of the first str segment that is made up only of characters from the string chars. When a character appears in the string str that isn't in the chars string, the function stops scanning the text.


Filename: StringLen.cpp


Length of initial segment: 12


In this case, the strspn() function will look for characters in the chars string by examining the str string. The number of characters at the start of str that matches any character in chars will be represented by the result that is returned.

Value Returned:

The function returns the total number of characters that chars contain at the beginning of str. In the event that str contains no characters from chars, 0 will be returned.

Apply Cases:

  • Input validation is a useful tool for verifying that user input contains only permitted characters.
  • Tokenizing Strings: This technique divides strings according to certain characters or delimiters.
  • Parsing data facilitates the extraction and analysis of particular segments of a string when working with structured data.

Warnings and Points to Remember:

  • String Null Termination: The strings str and chars have to be null-terminated.
  • Order and Duplication: The behavior of the function is influenced by the characters that make up chars. When it comes across the first character that is not in chars, it stops.
  • Complexity: The temporal complexity of the function is usually proportional to the product of the length of str and the length of chars.

The strspn() function is a straightforward yet effective tool for string analysis and modification in C++.

More Advanced Use:

Putting Custom String Tokenization into Practise:

Using strspn() along with other string methods like strtok() to create a unique tokenization function is one sophisticated use case:



Token: Hello
Token: world
Token: This
Token: is
Token: a
Token: test


In this case, the input string str is tokenized by the customTokenizer function using the characters found in the delimiters string. It uses strspn() to discover the segment containing delimiters and strcspn() to find the segment containing non-delimiters.

Performance Considerations:

While strspn() is a helpful function for string processing, it's crucial to consider its performance characteristics, especially in scenarios where it might be called repetitively or with lengthy strings.

Complexity and Optimization:

  • Time Complexity: The time complexity of strspn() typically depends on the length of both the str and chars. It traverses the str until it encounters a character not found in chars. In the worst case, it might run in O(m*n) time, where 'm' is the length of chars and 'n' is the length of str.
  • Optimization: For scenarios where performance is critical, consider optimizing string processing by minimizing the number of strspn() For instance, instead of repeated calls on the same string, one might operate once and cache the results for subsequent use, reducing redundant computations.

The strspn() method in C++ is a useful tool for string manipulations. It may determine the length of the initial part of a string that matches a certain set of characters, which serves as the foundation for the manipulation of string activities such as input verification, analysis, and tokenization. It can be used in combination with other operations on strings.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA