StrContains doesn't find certain strings

The following code shows an example where I was trying to find “dim” in a string.

For some reason the code will find “ddim” in Http_req, but will not find “dim”

To solve my problem I just changed my input data so the string contains x10cmd-dim and then search for -dim which works just fine. I just thought I’d see if someone had an answer as to why the search for “dim” doesn’t work.
I am using a mega with ide 1.0.3 and the following library’s.

Thanks for looking.
Mike

#include <DallasTemperature.h>
#include <Wire.h>
#include <DS1307new.h>
#include <OneWire.h>
//#include <EEPROM.h>
#include <EEPROMex.h>
#include <x10.h>
#include <x10constants.h>
#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SdFatUtil.h>
#include <EthernetUdp.h>
#include <string.h>
#include <Time.h>
#include “Smtp_Service.h”

}

void testcode()   //  /Z/    //Fx
  {  
  char Http_req[]  = "get x10cmddim,something,something";
  char Http_req2[] = "get x10cmd-dim,something,something";
 
 cout << pstr("\nSearch for dim");
 if ((StrContains(Http_req, "dim")))  {cout << pstr("\nFound dim\n");}  // /x10cmddim,name~,dims.
   else { cout << pstr("\nDid not find dim\n");  }
 cout << pstr("Search for ddim");  
 if ((StrContains(Http_req, "ddim")))  {cout << pstr("\nFound ddim\n");}  // /x10cmddim,name~,dims.
   else { cout << pstr("\nDid not find ddim\n");  }
 
 cout << pstr("Search for -dim");
 if ((StrContains(Http_req2, "-dim")))  // don't know why, but could not get search for just plane dim to work
        {  cout << pstr("\nFound -dim\n"); }
   else { cout << pstr("\nDid not find -dim\n");  }  [left][/left]

I have no idea what is that StrContains function, but it's not native. What's wrong with the native strstr?

if ( strstr( Http_req, "dim" ) ) // if string contains "dim"

If you’re using the version of StrContains that I found on the web there seems to be a weird bug in it - I don’t have time to dig into it right now.

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;
 
    len = strlen(str);
   
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }
 
    return 0;
}

strstr() works.

Pete

ok, thanks . I don't remember why i was using strContains, but tried strstr and works correctly.

Mike

I have found the bug in StrContains. The problem occurs if a leading substring of the sfind string occurs immediately before it in 'str'. In the case of searching str="get x10cmddim,something,something" for "dim" there is a 'd' immediately preceding "dim" in str. Searching str="get x10cmdidim,something,something" for "dim" would also fail. There's a simple fix for it but why bother when you have strstr() ?

Pete

I will change my code to use strstr. I guess the only reason to fix it would be to save some other poor sap from stumbling into the hole.

Thanks Mike