Hi,
I'm trying to make a function to count the occurences of a char or string in another string. (I know thee is a thread about this below but that is just for chars)
Please could someone tell me why my Instr() function does not work because I have no idea...
I presume it is to do with my mid() function.
Also, if you have time, would somebody please tell me how to free the memory in my mid function? (which is also pretty bad) Thanks very much. I'm a newbie and would appreciate any help (as long as I understand it )
char* Mid(char *Sentence, int FromNumber, int ToNumber)
{
char* newSentence = (char *) malloc(1000);
if (ToNumber == 0)
{
ToNumber = strlen(Sentence);
FromNumber = strlen(Sentence) - FromNumber;
}
ToNumber = ToNumber+FromNumber;
for (int i = (FromNumber--); i < ToNumber; i++)
{
*(newSentence+(i-FromNumber)) = *(Sentence+i);
}
*( newSentence + (ToNumber-FromNumber)) = 0;
return newSentence+1;
}
///////////////////////////////////////////////////////////
int Instr(char *SearchString, char *SearchTerm)
{
int ReturnValue = 0;
for (int i = 0 ; i <= strlen(SearchString)-strlen(SearchTerm); i++)
{
if (SearchTerm == Mid(SearchString, i, strlen(SearchTerm)))
{
ReturnValue ++;
}
}
return ReturnValue;
}
I'm trying to make a function to count the occurences of a char or string in another string. (I know thee is a thread about this below but that is just for chars)
Please could someone tell me why my Instr() function does not work because I have no idea...
I presume it is to do with my mid() function.
Also, if you have time, would somebody please tell me how to free the memory in my mid function? (which is also pretty bad) Thanks very much. I'm a newbie and would appreciate any help (as long as I understand it )
char* Mid(char *Sentence, int FromNumber, int ToNumber)
{
char* newSentence = (char *) malloc(1000);
if (ToNumber == 0)
{
ToNumber = strlen(Sentence);
FromNumber = strlen(Sentence) - FromNumber;
}
ToNumber = ToNumber+FromNumber;
for (int i = (FromNumber--); i < ToNumber; i++)
{
*(newSentence+(i-FromNumber)) = *(Sentence+i);
}
*( newSentence + (ToNumber-FromNumber)) = 0;
return newSentence+1;
}
///////////////////////////////////////////////////////////
int Instr(char *SearchString, char *SearchTerm)
{
int ReturnValue = 0;
for (int i = 0 ; i <= strlen(SearchString)-strlen(SearchTerm); i++)
{
if (SearchTerm == Mid(SearchString, i, strlen(SearchTerm)))
{
ReturnValue ++;
}
}
return ReturnValue;
}