This blog is under construction

Sunday, 29 April 2012

memchr example in C

Header file:
    string.h

Synopsis:
     void *memchr(const void *str, int ch, size_t n);

Description:
     It searches for the character ch in first num bytes of str and returns pointer to the first occurrence of the character ch in str on success.  Otherwise, NULL is returned.


memchr function C example:


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

  int main() {
        char ch, input[100], *strloc;
        printf("Enter your input: \n");
        fgets(input, 50, stdin);
        printf("Enter your char input:");
        ch = getchar();
        strloc = memchr(input, ch, strlen(input));
        if (strloc == NULL)
                printf("given character not found\n");
        else
                printf("Output: %s", strloc);
        return 0;
  }
 


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:
  see-programming.blogspot.in
  Enter your char input:b
  Output: blogspot.in

 

No comments:

Post a Comment