This blog is under construction

Sunday, 29 April 2012

strpbrk example in C

Header file:

     char *strpbrk(char *str, char *brk);

    Returns pointer to the first occurrence in string str of any of the characters present in string brk.  It returns NULL on failure.

strpbrk function C example:


  int main() {
        char input[100], brk[50], *ret;

        printf("Enter your input:");
        fgets(input, 50, stdin);
        printf("Enter a string for brk:");
        fgets(brk, 50, stdin);

        ret = strpbrk(input, brk);
        if (ret == NULL)
                printf("token not found in the given input\n");
                printf("Output: %s\n", ret);
        return 0;

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your
  Enter a string for brk:zb

Here, we have given two strings "" and "zb" as input. Character 'z' in second input string is not present in first input string.  Whereas, the next character 'b' is present in the first input string.  So, the pointer to first occurrence of 'b' in first input string is returned.

No comments:

Post a Comment