Returns the index of the first occurrence of a character in a string that belongs to a set of characters using strcspn()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Returns the index of the first occurrence of a character in a string that belongs to a set of characters using strcspn() C function

To show: How to use strcspn() C function to find the index of the first occurrence of a character in a string that belongs to a set of characters

 

 

// Using strcspn()/wcscspn()/_mbscspn()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *string1 = "The value is 3.14159";

char *string2 = "1234567890";

 

printf(" Using strcspn()\n");

printf(" ---------------\n");

printf("string1 = %s\n", string1);

printf("string2 = %s\n", string2);

printf("\nThe length of the initial segment of string1\n");

printf("not containing characters from string2 = %u", strcspn(string1, string2));

printf("\n");

 

return 0;

}

 

Output example:

 

Using strcspn()

---------------

string1 = The value is 3.14159

string2 = 1234567890

The length of the initial segment of string1

not containing characters from string2 = 13

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index