This blog is under construction

Sunday, 29 April 2012

strcspn example in C

 Header file:
    string.h

Synopsis:
   size_t strcspn(char *str, char *rej);

Description:
   It returns the length of the prefix in string str which consists of characters not in string rej.


strcspn function C example:


  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>

  int main() {
        char input[100];
        int size;

        printf("Enter your input:");
        fgets(input, 50, stdin);
        size = strcspn(input, "abcdefghijklmnopqrstuvwxyz");
        printf("Output: %d\n", size);
        return 0;
  }
 


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:12345helloworld
  Output: 5


Here, the input string is "12345helloworld"  and the other string rej is "abcdefghijklmnopqrstuvwxyz".  Characters in the prefix ("12345")of input string is not available in the string rej.  So, the length of "12345" is returned as return value.


No comments:

Post a Comment