This blog is under construction

Sunday, 29 April 2012

strstr example in C

Header file:
    string.h

Synopsis:
    char *strstr(char *str, char *substr);

Description:
   It returns pointer to the first occurrence of string substr in string strNULL is returned, if string substr is not available in string str.


strstr function C example:


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

  int main() {
        char *str, *res;
        str = (char *) malloc(100);

        printf("enter your string:");
        fgets(str, 80, stdin);

        res = strstr(str, "blogspot");  // checks blogspot is substring of str
        if (res == NULL)
                printf("Substring absent\n");
        else
                printf("Substring present: %s\n", res);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  enter your string:see-programming.blogspot.in
  Substring present: blogspot.in




1 comment: