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?
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;
}
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.
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.
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.
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.