strcspn(const char *str1, const char *str2)
Parameters or arguments used in this function:
str1:
The string to be searched for, also known as the target string.
str2:
Characters from the input string are utilized to compare against the target string.
Value Returned:
This function determines the count of characters preceding the initial common character in both strings.
Example:
// To demonstrate how strcspn() works in C
#include <stdio.h>
#include <string.h>
int main()
{
int size;
// initializing strings
char str1[] = "logic practice";
char str2[] = "mayfair";
// Using strcspn() to determine the beginning characters before the first matching character produces 3
size = strcspn(str1, str2);
printf("The unmatched characters before first matched character : %d\n", size);
}
Output:
The unmatched characters before first matched character : 1
Another example with detailed explanation:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, india!";
char str2[] = "llo";
size_t length = strcspn(str1, str2);
printf("The length of the initial segment of str1 that does not contain any characters from str2 is: %zu\n", length);
return 0;
}
Output:
The length of the initial segment of str1 that does not contain any characters from str2 is: 2
Explanation:
The initial portion of a string that does not include any specified characters is calculated by using the strcspn function.
In this specific case, we are working with two distinct strings: str1 and str2. The string str1 contains the phrase "Hello, india", whereas str2 consists of the characters "llo". Our objective is to determine the length of the initial segment of str1 that does not contain any characters from str2.
The strcspn function takes two parameters: str1 represents the string to be searched, while str2 consists of the characters to be searched for. It returns the length of the initial segment in str1 that contains no characters from str2. For instance, since the string "Hell" in str1 does not contain any characters from str2, the function will return 7 as the length in this scenario.
Following that, the original segment's size is shown by outputting the result through the printf function. Whenever there is a necessity to calculate the length of a substring while excluding certain characters, the strcspn function proves to be valuable.
Relevant Application:
This function is applicable in various real-world scenarios, such as word puzzles and anomaly detection tools. The following tutorial illustrates a simple word puzzle.
Rules:
In this particular game, a pair of participants are involved, with the initial player assigned the challenge of forming a string containing the highest number of unpaired characters. At the end of a single round, the individual who crafted the lengthiest sequence of unmatched characters emerged victorious.
// To demonstrate how strcspn() works in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int score1 = 0, score2 = 0, k = 0, sizen = 0, size = 0;
char player1[] = "javat";
char play2[] = "";
while (1) {
// random characters are generated
char randoml = 'a' + (random() % 26);
play2[k++] = randoml;
size = strcspn(play2, player1);
if (size == sizen) {
// it will break if there is any character
score2 = size;
break;
}
else {
sizen = size;
}
}
// round2 string initialization
const char player2[] = "point";
char play1[] = "";
k = 0, sizen = 0;
while (1) {
// random characters are generated
char randoml = 'a' + (random() % 26);
play1[k++] = randoml;
size = strcspn(play1, player2);
if (size == sizen) {
// it will break if there is any character
score1 = size;
break;
}
else {
sizen = size;
}
}
if (score1 > score2)
printf("Player 1 won!! Score : %d", score1);
else if (score2 > score1)
printf("Player 2 won!! Score : %d", score2);
else
printf("Match Drawn!! Score : %d", score1);
}
Output:
Match Drawn!! Score : 1