Strstr Implementation in C

StrStr : Locate SubString in String
Returns a pointer to the first occurrence of a substring within another string.
Syntax ptr = strstr( s, subs );
const char *s : points to the string to be scanned.
const char *subs : points to the (sub)string to scan for.
char *ptr : points to the first occurrence of the substring in the given string. If the substring is not found, this will be a null pointer.

strstr(const char *in, const char *str){
   char c;
   size_t len;
   c = *str++;
   if (!c)
   return (char *) in;
   len = strlen(str);
   do {
      char sc;
      do {
         sc = *in++;
         if (!sc)
            return (char *) 0;
      } while (sc != c);
   } while (strncmp(in, str, len) != 0);
   return (char *) (in – 1);
}

About mohan

This is Mohan Jayapalan author of TechPages Blog.I am a part time blogger and Currently working for private Software Concern.

© Copyright 2010-2016 www.techpages.org · All Rights Reserved · Powered by WordPress ·