This blog is under construction

Sunday, 29 April 2012

strpbrk example in C

Header file:
    string.h

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

Description:
    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:


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

  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");
        else
                printf("Output: %s\n", ret);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:see-programming.blogspot.com
  Enter a string for brk:zb
  Output: blogspot.com


Here, we have given two strings "see-programming.blogspot.com" 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