Hey guys, I have the following two pieces of code and I am trying to combine them so it will say if hello is found and it is at the start or the prevous character is whitespace then add one to the count but when i try to combine these two pieces of code to do that my syntax is all wrong.

Any help would be great. Thanks.

char * ch = array;
while((ch = strstr( ch, "hello")) != 0)
{
 Count++;
ch++;
}
if(ch == array || isspace(*(ch-1)))
{
	count++					
	ch++							
	}

Recommended Answers

All 2 Replies

char * ch = array;
while((ch = strstr( ch, "hello")) != 0) {
    if(ch != array && !isspace(ch[-1])) continue;
    Count++;
    ch++;
}

That should work.

And yes, array[-1] is valid. :)

isspace() is in <ctype.h> (or, in C++, <cctype>).

And yes, array[-1] is valid. :)

It may be valid, but I am starting to see some modern compilers complain about it.

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.