Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instr() function 2

Status
Not open for further replies.

kriscs1

Programmer
Jul 5, 2005
12
GB
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 :p)


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;
}
 
hmmm how stupid of me.... It's just that I saw a tutorial on strncpy_s() which showed sizeof() there and I just automatically added it to mine. thansk very much!
Works fine now :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top