hey guys, I am using the following code to count from a text file but I also want to check for ",jo " as well as "jo " I was trying to do it by using while((ch = strstr( ch, "jo ")) != 0) || while((ch = strstr( ch, ",jo ")) != 0) but this doesn't work I was just wondering how I could so this thanks.

char *ch=array;
while((ch = strstr( ch, "jo ")) != 0) 

	if( ch == array '||isspace (*(ch-1)))
		{
			temp.count++;
		      ch++;
													
			
	}
		else
                    {
                        break;
                     }

}

Dani AI

Generated

Short answer: the crash was almost certainly caused by evaluating two strstr assignments in one || expression and then passing a NULL pointer into strstr (or by dereferencing found[-1] at the start of the buffer). As noted, the combined condition is syntactically valid, but when the left strstr assignment yields NULL the right side may still be evaluated and receive that NULL — calling strstr with a null haystack is undefined behavior. See the strstr notes for the rule about null pointers. strstr documentation

A safe pattern is to call the two searches into temporary pointers, pick the earliest non-null result, then update your main search pointer only after you know which result to use. Also always guard any found[-1] use (check found > base) and pass isspace an unsigned char to avoid undefined behavior. Example:

const char *base = text;
const char *cur = base;
const char *r1, *r2, *found;

while (cur && *cur) {
    r1 = strstr(cur, "jo ");
    r2 = strstr(cur, ",jo ");
    if (!r1 && !r2) break;
    found = (!r1 || (r2 && r2 < r1)) ? r2 : r1;
    if (found == base || (found > base && isspace((unsigned char)found[-1]))) {
        ++count;
    }
    cur = found + 1; /* or found + match_length to skip past the token */
}

Quick troubleshooting tips: 1) Never call string functions with a NULL pointer. 2) Check found > base before reading found[-1]. 3) Use isspace((unsigned char)...). 4) Advance the search pointer by the token length to avoid re-matching the same spot (or by 1 if overlaps are desired). Running under Valgrind or a debugger will quickly confirm illegal reads/writes if the crash persists.

Recommended Answers

All 4 Replies

Use this.

while( ( (ch = strstr( ch, "jo ") ) != 0) ||( (ch = strstr( ch, ",jo ")) != 0 ))

Note:
I am not sure about the logic, that is your responsibility. But the syntax is okay.

Hi thanks for your reply the logic seems to be working alright for me. What do you think was wrong with it?

Thanks.

Nothing. I said that because I didnt know your requirements. If it meets your requirements then it is correct.

Nothing. I said that because I didnt know your requirements. If it meets your requirements then it is correct.

Thanks in the end I couldn't use the || even with the correct syntax and just has to use another while loop or for some reason the program would crash. Not sure why it was crashing though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.