BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl
Home / c-programming / C strcspn() Function – C tutorial

C strcspn() Function – C tutorial

By Chaitanya Singh

The strcspn() function scans the main string for the given string and returns the number of characters in the main string from beginning till the first matched character is found.

C strcspn() declaration

size_t strcspn(const char *str1, const char *str2)

str1 – The main string to be searched
str2 – The characters of this string is searched in the main string till the first matched character is found

Return value of function strcspn()

This function returns the number of characters in the main string that are found before a first matched character is found.

Function strcspn() example in C

#include <stdio.h>
#include <string.h>
int main () {
   const char str[20] = "aabbccddeeff"; 
   const char searchString[10] = "dxz";
   int loc;

   /* This function returns the number of characters present in the main string 
    * from beginning till the first matched character is found 
    */
   loc = strcspn(str, searchString);
   printf("The first matched char in string str1 is at: %d", (loc+1));
   return 0;
}

Output:

The first matched char in string str1 is at: 7

The characters that we are searching in the main string str are ‘d’, ‘x’ and ‘z’, the first matched character is found at the position 7 in the main string.

Related Posts:

  1. C – strcat() function
  2. C – strchr() function
  3. C – strcmp() function
  4. C – strcpy() function

Posted Under: c-programming | Tags: C-Library, C-String

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap