Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C strspn() Function

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

Photo Credit to CodeToFun

🙋 Introduction

In the C programming language, string manipulation is a common and essential task.

The strspn() function is a standard library function that calculates the length of the initial segment of a string consisting of only the characters contained in a specified set.

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

💡 Syntax

The syntax for the strspn() function is as follows:

Syntax
Copied
Copy To Clipboard
size_t strspn(const char *str, const char *charset);
  • str: The string to be examined.
  • charset: A null-terminated string containing the characters to match.

📄 Example

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

strspn.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  const char * string = "Hello, World!";
  const char * charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  size_t length = strspn(string, charset);

  printf("Length of initial segment containing only alphabet characters: %zu\n", length);

  return 0;
}

đŸ’ģ Output

Output
Length of initial segment containing only alphabet characters: 5

🧠 How the Program Works

In this example, the strspn() function calculates the length of the initial segment of the string "Hello, World!" that consists only of alphabet characters. The result is then printed.

↩ī¸ Return Value

The strspn() function returns the length of the initial segment of the string str that consists only of characters present in charset.

📚 Common Use Cases

The strspn() function is particularly useful when you need to validate or extract specific segments of a string based on a predefined set of characters. It can be employed in scenarios such as input validation or parsing.

📝 Notes

  • The strspn() function stops counting characters as soon as it encounters a character not present in charset.
  • If the initial character of str is not in charset, the function returns zero.

đŸŽĸ Optimization

The strspn() function is generally efficient for its intended purpose. However, for large strings and sets, consider optimizing the algorithm based on the specific use case.

🎉 Conclusion

The strspn() function in C is a valuable tool for working with strings and character sets. It provides a convenient way to calculate the length of a substring that consists only of specified characters.

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
11 months ago

If you have any doubts regarding this article (C 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