This blog is under construction

Sunday, 29 April 2012

strcspn example in C

 Header file:

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

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

strcspn function C example:


  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;

  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