Does String-A contain String-B

Pages: 12
This is a homework question so please do not give me an exact answer. I want to figure this out on my own for the most part so that I absorb and memorize this information.

I am asked to write a code that lets you know if String-A contains String-B.
An example given is that String-A == pineapple && string-B == linen. This would return true since pineapple has 'l' , 'i' , 'n' , 'e'. (Doesn't need both 'n' to be true).

The code I wrote will only return true/false based on the last letter it checks. I know I need a way for returned variable to change based on all instances of the check.

1
2
3
4
5
6
7
8
9
10
11
bool hasEach(const string & a, const string & b) {
	bool ret;

	for (unsigned pos = 0; pos < a.size(); pos++) {
		for (unsigned pos1 = 0; pos1 < b.size(); pos1++) {
			if (b[pos1] == a[pos])
				ret = true;
		}
	}
	return ret;
}


This is what I have so far. It loops through the first string and then loops through the second string and compares each letter of the second string to the first letter of the first string, then the second letter of the first string and so forth.

Any help, without a direct answer, would be appreciated.
Hmm. I see your train of thought. How about thinking of it like you search in a word search? Once you find the first letter of what you're looking for....what do you do next?
I'm guessing it only returns true based on the last letter since ret will always be true, and once your loop is finished THEN it returns true

why not juts do

return true inside if(b == a)? this will return true once it finds the first comparison
In a word search, you'd look or the second letter next to the one you just found. However, in this example, the letters aren't guaranteed to be in consecutive order. I did just change my code, and in theory, it should work. However, I'm not sure why it is giving me an error.

1
2
3
4
5
6
7
8
9
10
11
bool hasEach(const string & a, const string & b) {
	bool ret;

	for (unsigned pos = 0; pos < b.size(); pos++) {
		for (unsigned pos1 = 0; pos1 < a.size(); pos1++) {
			if (b[pos1] == a[pos])
				ret = true;
		}
	}
	return ret;
}


Any idea why it's not working?
1. What does hasEach("abcd", "efgh") return? Why?
2. What does hasEach("abcd", "afgh") return? Why?
Last edited on
OH!

Ok, I was thinking they had to be in consecutive order.

Well, your index is going out of bounds. Should you use b[pos1] == a[pos] or b[pos] == a[pos1]?
Alright. I've got a working solution that has very few changes from your code. I only added - I didn't delete anything you've already written. You're only missing a few boolean operations here.

Still need to fix the index going out of bounds, though. I fixed that on my side. Look at my comment above for help on that.
Changed my code yet again. I fixed the b[pos] and a[pos1] issue before I refreshed this page but thank you for pointing that out.

Helios, It needs to return ret. Whether its true or false. (1 or 0)

my main() includes cout <<hasEach("pineapple", "linen") <<endl;

So my code is meant to search through all letters of "pineapple" to see if it matches the first letter of "linen". Please check this code and let me know why it is still not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool hasEach(const string & a, const string & b) {
	bool ret;
	int count;

	for (unsigned pos = 0; pos < b.size(); pos++) {
		count = 0;
		for (unsigned pos1 = 0; pos1 < a.size(); pos1++) {
			if (b[pos] == a[pos1])
				count++;
		}
		if (count > 1)
			ret = true;
		else {
			ret = false;
			return ret;
		}
	}

	return ret;
}
i may be wrong but why do you switch the sizes in the for loop? the second for loop definitely hits out of bounds
OK. You can use a 'count' as well. Perhaps more intuitive than my solution.

You should only increase count if it finds that character. But what if it finds two instances of that character in string A? It will count up by 2 instead of 1! So, you don't want to keep searching for the current character if you've found it already. So.... after you do 'count++' you should do what?


And at the very end, what should 'count' be equal to in order for you to return true?
If you are only trying to match the first letter, you just need one for loop which iterates through "pineapple" and compares it to just b[0] which is the index to the first letter

Why do you need count? Just return true if you found it and boom, you're done
But it needs to match every character from string B to a character in string A. So every loop needs to result in a successful match to return true. Otherwise return false.
You should only increase count if it finds that character. But what if it finds two instances of that character in string A? It will count up by 2 instead of 1! So, you don't want to keep searching for the current character if you've found it already. So.... after you do 'count++' you should do what?


It already is set to increase count only when the letter matches. Also, even if it finds 2 separate instances of the letter in the string, it is fine. It still holds true. So that is why I had the if (count > 1) statement. the else statement should only return false if there were no instances of the letter, yet it still returns false every time.

With my own knowledge of coding, my code seems like it should work. It just simply doesn't.
Sorry but OP can you explain EXACTLY what you are trying to do?

To my understanding, you want to iterate through string a, and compare each letter to the first letter of string b yes?

for (int i = 0; i < a.size(); i++)
{
if (a[i] == b[0])
return true
}

isn't this it?
If that's what I was trying to do, it would be that simple. The first post I made explains exactly what I need to do.
"So my code is meant to search through all letters of "pineapple" to see if it matches the first letter of "linen". "

this is what you said...
Hmm, ok. You're using your count in a different way than I thought you were going to. I thought you were going to return the number of matches that you found.

count > 1
You still want it to match if you found 1 match, right? So, shouldn't count also be allowed to be EQUAL to 1? Fix this, and it will work.
Last edited on
if you want to see if all the letters of b are in a, a simple but unorthodox method is to make an int count = 0;

and it should return true if count == b.size(), so:

for(int i = 0; i < a.size(); i++)
{
for (int j = 0; j < b.size(); j++)
{
if (a[i] == b[j])
count++;
if (count == b.size())
return true
}
}
That was just explaining the first instance. If you look at the code I wrote, it cycles through all characters in string b. The code is meant to see if string a contains all letters of string b.

JayHawk, I just saw your comment above that said you found a working solution that just included a few more boolean expressions. I can't seem to think of where I would include anything else. My code really does seem right to me. My logic is failing me.
Why if count > 1 you return true?

This is just saying, if you found 2 letters of b in a, you return true, yet you say you need ALL letters in b

read the code I posted...
Pages: 12