Does String-A contain String-B

Pages: 12
Terribad, you're so close! You just have to change 'count > 1'. You need to add a singe symbol.

What if you found one match? Shouldn't that also be true? But that inequality will make it return false...

Like I said. You're really close. You don't need to change it and start over. Your implementation will work with one more symbol added on one line.
Last edited on
btw in your code, you put count = 0 INSIDE the for loop, which means no matter how many times you find a match and do count ++, it will always iterate back to 0
I've tested it with the change I suggested, and I can verify that it will work.

'count' needs to be set back to zero after every loop iteration. What he's doing is making 'ret' false if count is never incremented during the inside loop. But 'count' needs to count fresh for each character in stirngB
oo yes that is true disregard what i said

but i still dont understand why you dont just do, if count == b.size(), return true?
so you're saying to put count=0; inside the first for loop but AFTER the second for loop?

Both of you two are showing me two different ways.

Alex, the reason why your code wouldn't work for this is because if the tables were turned and string - a was pinneapple and string - b was line, then there would count would be == 5. Which is != b.size() in this case.
LOL WOW.

Just got it Jayhawk... I thought I had it say (count > 0). So dumb.. Literally one simple mistake and the whole code doesn't work. I just got it working. Even with the count=0 still inside the first for loop.

Thank you both for all the help!
XD

You're welcome.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	bool found = false;
	std::string stringsA[] = { "teststring1", "pineapple", "turtle", "imbadatmakingexamples" };
	std::string stringsB[] = { "test", "linen", "purple", "makebats" };

	for (auto i = 0; i < 4; i++)
	{
		found = hasEach2(stringsA[i], stringsB[i]);
		std::cout << "Iteration One: " << found << " | " << stringsA[i] << (found ? "' has '" : "' does not have '") << stringsB[i] << "'" << std::endl;
	}
}


Produces
Iteration One: 1 | teststring1' has 'test'
Iteration One: 1 | pineapple' has 'linen'
Iteration One: 0 | turtle' does not have 'purple'
Iteration One: 1 | imbadatmakingexamples' has 'makebats'
Press any key to continue . . .


You can use that for lots of tests really fast. But now you know that it works :P

Welcome to the world of programming. Little errors like that are actually very common.
Last edited on
Its always so easy once the function has already been made haha.

Thanks again! Luckily this was my last assignment of the semester. However, if it takes me this long to recreate the code in class, I'm screwed haha.
The more practice you get, the easier it will be to come up with solutions. You'll gain an intuition for it.
Topic archived. No new replies allowed.
Pages: 12