I'm making a substring function. When I call it the prog hangs and I don't understand why Heres my function:
Code:
int String::Sub(char *sub, bool case_sensitive=true)
{
//Searches for a substring within the string
//Returns -1 for failure or the index of first occurence
bool in=false;
char *a='\0', *b='\0';
int found=-2; //if returns -2 then somethings wrong
for(int i=0; i<size; i++)
{
a=&str_ptr[i];
if(!case_sensitive)
if(*a >= 'A' && *a <= 'Z')
*a+=32;
if(!in)
{
b=sub;
if(!case_sensitive)
if(*b >= 'A' && *b <= 'Z')
*b+=32;
if(*a == *b)
{
found=i;
in=true;
}
}
else
{
b++;
if(*b == '\0') return found;
if(!case_sensitive)
if(*b >= 'A' && *b <= 'Z')
*b+=32;
if(*a != *b)
{
in=false;
i=found+1;
}
}
}
return -1;
}
If I comment out the red section it works fine, but it won't do a non case sensitive comparison properly. The thing is it works fine for all the other instances where I convert to lower case. Also heres my main function with headers and stuff excluded:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
String something("Something\n");
something.Print();
something.Add("Else\n");
cout << something.Sub("ELSE", false);
cin.ignore();
return 0;
}
Anyone know why this is happening?