C - strcspn() function
C strcspn() function - get the length of a complementary substring
The strcspn() function is used to find the first occurrence of a character in s1 that belongs to the set of characters that is specified by s2.
Syntax:
size_t strcspn(const char *s1, const char *s2);
Parameters:
Name | Description | Required /Optional |
---|---|---|
s1 | Null-terminated searched string. | Required |
s2 | Null-terminated character set. | Required |
Return value from strcspn()
- The strcspn() function returns the index of the first character found.
- Return the length of the string if no match found.
- No return value is reserved to indicate an error.
Examples: strcspn() function
Example 1: How strcspn() locates the index of specific characters in a given string?
This example demonstrates how strcspn() locates the index of specific characters in a given string. It searches for individual letters ("C", "L", "a", "g", and "U") within "C Language," outputting the index for each. This showcases how strcspn() can efficiently determine the position of various characters within a string.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char string1[SIZE] = "C Language";
char ch;
int index;
printf("Original string: %s", string1);
index = strcspn(string1, "C");
printf("\n\nIndex of character 'C' in the said string is %d.",index);
index = strcspn(string1, "L");
printf("\n\nIndex of character 'L' in the said string is %d.",index);
index = strcspn(string1, "a");
printf("\n\nIndex of character 'a' in the said string is %d.",index);
index = strcspn(string1, "g");
printf("\n\nIndex of character 'g' in the said string is %d.",index);
index = strcspn(string1, "U");
printf("\n\nIndex of character 'U' in the said string is %d.",index);
}
Output:
Original string: C Language Index of character 'C' in the said string is 0. Index of character 'L' in the said string is 2. Index of character 'a' in the said string is 3. Index of character 'g' in the said string is 5. Index of character 'U' in the said string is 10.
Example 2: Find position of the first vowel in a word
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a string to search within
char text[] = "programming";
// Define a set of vowels to search for
char vowels[] = "aeiou";
// Find the position of the first vowel
int position = strcspn(text, vowels);
// Print the position if a vowel is found
if (position < strlen(text))
printf("First vowel in \"%s\" found at position %d.\n", text, position);
else
printf("No vowels found in \"%s\".\n", text);
return 0;
}
Output:
First vowel in "programming" found at position 2.
Explanation:
- Defines text with "programming" and vowels as "aeiou".
- strcspn() searches text for any character in vowels.
- Displays the position of the first vowel, if found.
Example 3: Find first forbidden character in password
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Password to check
char password[] = "secure#pass";
// Forbidden characters for passwords
char forbidden[] = "#$%^&*";
// Find the first occurrence of a forbidden character
int position = strcspn(password, forbidden);
// Print a warning if a forbidden character is found
if (position < strlen(password))
printf("Forbidden character found at position %d in \"%s\".\n", position, password);
else
printf("No forbidden characters found in \"%s\".\n", password);
return 0;
}
Output:
Forbidden character found at position 6 in "secure#pass".
Explanation:
- Defines password with "secure#pass" and forbidden as "#$%^&*".
- Uses strcspn() to locate any forbidden characters in password.
- Alerts the user if a forbidden character is found by its position.
C Programming Code Editor:
Previous C Programming: C strncpy()
Next C Programming: C strerror()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-strcspn.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics