Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strspn() Function

Posted in C++ Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 158 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C++ String strspn() Function

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, manipulating and analyzing strings is a common and crucial task.

The strspn() function is part of the C Standard Library and is used to find the length of the initial segment of a string consisting of characters that are part of a specified set.

In this tutorial, we'll explore the usage and functionality of the strspn() function in C++.

💡 Syntax

The signature of the strspn() function is as follows:

Syntax
Copied
Copy To Clipboard
size_t strspn(const char *str, const char *charset);

This function takes two parameters:

  • str: A pointer to the string to be analyzed.
  • charset: A pointer to the null-terminated set of characters to search for.

📄 Example

Let's delve into an example to illustrate how the strspn() function works.

strspn.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cstring>

int main() {
  const char * str = "C++ is awesome!";
  const char * charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

  // Find the length of the initial segment of str consisting of characters in charset
  size_t length = strspn(str, charset);

  // Output the result
  std::cout << "Length of the initial segment: " << length << std::endl;

  return 0;
}

đŸ’ģ Output

Output
Length of the initial segment: 1

🧠 How the Program Works

In this example, the strspn() function is used to find the length of the initial segment of the string "C++ is awesome!" consisting of characters in the specified character set.

↩ī¸ Return Value

The strspn() function returns the length of the initial segment of str consisting of characters that are part of charset.

📚 Common Use Cases

The strspn() function is useful when you need to validate or analyze strings based on a specific set of characters. It's commonly used for tasks like input validation or parsing.

📝 Notes

  • The strspn() function stops counting when it encounters a character that is not present in the charset.
  • If the initial character of str is not part of charset, the function returns 0.

đŸŽĸ Optimization

The strspn() function is optimized for finding the length of the initial segment efficiently. There is generally no need for additional optimization.

🎉 Conclusion

The strspn() function in C++ provides a straightforward way to find the length of the initial segment of a string consisting of characters in a specified set. It is a valuable tool for string manipulation and analysis tasks.

Feel free to experiment with different strings and character sets to explore the behavior of the strspn() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (C++ String strspn() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy