Advertisement

Question about Tutorial 22 - isInString Function

Started by January 27, 2003 02:12 PM
1 comment, last by UltimateZeus 22 years, 1 month ago
Hi, This code is in Tutorial 22. As far as I can make out, it is supposed to find out if there is a substring within another string. Is this right? I have tested the code with some different strings and it always seems to return TRUE. Any suggestions? bool isInString(char *string, const char *search) { int pos=0; int maxpos=strlen(search)-1; int len=strlen(string); char *other; for (int i=0; i1) && string[i-1]==''\n'')) { other=&string; pos=0; while (string!=''\n'') { if (string==search[pos]) pos++; if ((pos>maxpos) && string[i+1]==''\n'') return true; i++; } } } return false; } </i>
#include <string.h>bool isInString(char *string, const char *search){    char *p;    p = strstr(string,search);    while((p != NULL) && (*(p+strlen(search)) != '\n') && ((*(p+strlen(search)) != '\0'))    {        p = strstr(string,search);    }    return (p != NULL);} 


[edited by - Extrarius on January 27, 2003 4:23:30 PM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Advertisement
Thanks. What is the :

while((p != NULL) && (*(p+strlen(search)) != ''\n'')
&& ((*(p+strlen(search)) != ''\0''))
{
p = strstr(string,search);
}

for. Can you not leave this while loop out (what does it do exactly?

I thought the strstr command looks through the string even if there are \n escape characters

This topic is closed to new replies.

Advertisement