This is strstr() function made by me as given in the practice problem.

char *cstrstr(char s[],char ss[])
{
 int l1,l2,i,j,k,flag=0;
 char *p=NULL;
 l1=strlen(s);
 l2=strlen(ss);
 for(i=0;i<l1;i++)
 {
  //cout<<"i:"<<i;
  p=&s[i];
  if(ss[0]==s[i])
  {
  for(k=i,j=0;j<l2;j++,k++)
  {
   //cout<<"j:"<<j<<"k:"<<k;
   if(ss[j]==s[k])
   {
    flag=1;
    continue;
   } 
   else
   {
    flag=0;
    break;
   }
  }
  }
 if(flag)
 return p;
 }
 return NULL;
}

Plz verify it. Any suggestions,comment or recommendations are welcomed.
[I'm between a beginner and a pro in c++]

Everything seems to work fine, although I don't particularly like the way you have formatted it (white space missed alot and its all very streched) but thats a matter of personal opinion. There were also a couple of compiler warnings to do with assigning l1 and l2 (int's) with size_t variables recieved from the strlen function. To fix that I just changed their data types to size_t aswell.

char *cstrstr(char s[],char ss[]) {
   size_t l1, l2, i, j, k;
   int flag = 0;
   char *p = NULL;
   l1 = strlen(s);
   l2 = strlen(ss);
   for (i = 0; i < l1; i++) {
      p = &s[i];
      if (ss[0] == s[i]) {
         for (k = i, j = 0; j < l2; j++, k++) {
            if (ss[j] == s[k]) {
               flag = 1;
               continue;
            } else {
               flag = 0;
               break;
            }
         }
      }
      if (flag) return p;
   }
   return NULL;
}

Hope this helps.

Thanks William :) for your suggestions. I liked them. Can you tell me more about size_t (is it the longest unsigned integer?) And are there any suggestions for shorting the code, bcoz i think i coded it a bit lengthier way.

size_t is just a typedef'd unsigned int which can only hold absolute integers, because a string for example will never have a negative amount of characters. As for shortening the code, there really isn't that much you can do as far as I can see :P

char *cstrstr(char s[],char ss[]) {
   size_t l1, l2, i, j, k;
   int flag = 0;
   char *p = NULL;
   l1 = strlen(s);
   l2 = strlen(ss);
   for (i = 0; i < l1; i++) {
      p = &s[i];
      if (ss[0] == s[i]) {
         for (k = i, j = 0; j < l2; j++, k++) {
            if (ss[j] == s[k]) {
               flag = 1;
               continue;
            } else {
               flag = 0;
               break;
            }
         }
      }
      if (flag) return p;
   }
   return NULL;
}

It worked for me. One slight optimization could be done, I think. Look at lines 9 through 11 above.

if (ss[0] == s[i]) {
         for (k = i, j = 0; j < l2; j++, k++) {
            if (ss[j] == s[k]) {

The fist time through the for-loop, k = i and j = 0, so this line:

if (ss[j] == s[k]) {

is this line the first time through the loop:

if (ss[0] == s[i]) {

Notice that you've already tested before you got to the loop:

if (ss[0] == s[i]) {
         for (k = i, j = 0; j < l2; j++, k++) {
            if (ss[j] == s[k]) {

so it'll always be true. Thus, a slight optimization might be to change the for-loop to this:

for (k = i + 1, j = 1; j < l2; j++, k++) {

to avoid the redundant test. It's a fairly minor issue though.

I noticed that recheck , but i did something wrong in the second loop while correcting that. Therefore to avoid further error, i implemented it this way.
Thanks VernonDozier for optimized code. :)

This article has been dead for over six months. Start a new discussion instead.