The strcspn function in C is useful for finding the length of an initial segment of a string that does not contain any characters from another string. In this blog, we’ll explore strcspn, its usage, and several examples to help beginners understand its application.
What is strcspn?
The strcspn function calculates the length of the initial segment of a string that consists entirely of characters not in another string. Here’s the syntax for strcspn:
size_t strcspn(const char *str1, const char *str2);
str1: Pointer to the null-terminated string to be scanned.str2: Pointer to the null-terminated string containing the characters to match.
Return Value:
- The function returns the number of characters in the initial segment of
str1which are not present instr2.
How strcspn Works
The strcspn function scans str1 and counts the number of characters that are not in str2. The scanning stops as soon as a character from str2 is found in str1.
Example of strcspn:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = " ,!";
size_t result = strcspn(str1, str2);
printf("Length of segment in '%s' containing none of '%s' is %zu.\n", str1, str2, result);
return 0;
}
Output:
Length of segment in 'Hello, World!' containing none of ' ,!' is 5.
Additional Examples
Let’s explore three more examples to understand the practical use of strcspn.
Example 1: Finding Length Before Digits
Suppose you want to find the length of the initial segment of a string that contains no digits.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello123World";
char str2[] = "0123456789";
size_t result = strcspn(str1, str2);
printf("Length of segment in '%s' containing none of '%s' is %zu.\n", str1, str2, result);
return 0;
}
Explanation:
str1is “Hello123World”.str2contains all digits “0123456789”.- The function scans
str1until it finds a digit (which is ‘1’ at position 5).
Output:
Length of segment in 'Hello123World' containing none of '0123456789' is 5.
Example 2: Extracting Filename from Path
You can use strcspn to extract the filename from a given file path.
#include <stdio.h>
#include <string.h>
int main() {
char path[] = "/usr/local/bin/program";
char delimiters[] = "/";
size_t result = strcspn(path, delimiters);
char filename[100];
strncpy(filename, path + result + 1, strlen(path) - result);
filename[strlen(path) - result - 1] = '\0';
printf("Filename: %s\n", filename);
return 0;
}
Explanation:
pathis “/usr/local/bin/program”.delimitersis “/” to find the last segment after the last slash.- The function scans
pathand stops at the first occurrence of ‘/’.
Output:
Filename: usr/local/bin/program
Example 3: Finding the First Vowel
You can use strcspn to find the first vowel in a string.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "bcdfghjklmnpqrstvwxyzAEIOU";
char vowels[] = "AEIOUaeiou";
size_t result = strcspn(str1, vowels);
if (str1[result] != '\0') {
printf("First vowel in '%s' is '%c' at position %zu.\n", str1, str1[result], result);
} else {
printf("No vowels found in '%s'.\n", str1);
}
return 0;
}
Explanation:
str1is “bcdfghjklmnpqrstvwxyzAEIOU”.vowelsis “AEIOUaeiou”.- The function scans
str1until it finds the first vowel (which is ‘A’ at position 21).
Output:
First vowel in 'bcdfghjklmnpqrstvwxyzAEIOU' is 'A' at position 21.
Visual Diagram
To better understand how strcspn works, let’s use a simple ASCII-style diagram for Example 1:
str1: "Hello123World"
str2: "0123456789"
Initial segment of str1 not containing any characters from str2:
+---+---+---+---+---+---+---+---+---+---+---+---+---+
| H | e | l | l | o | 1 | 2 | 3 | W | o | r | l | d |
+---+---+---+---+---+---+---+---+---+---+---+---+---+
|<--- segment length (5) --->|
Conclusion
The strcspn function is a powerful tool in C for finding the length of an initial segment of a string that doesn’t contain any characters from a specified set. It can be used for various purposes, such as extracting substrings, finding prefixes, and locating specific characters. Understanding how strcspn works and its potential use cases will enhance your string manipulation skills in C programming.
Happy coding!