problem help c++
Some help with this problem:
Code:
Write a function that counts the number of occurrences of a pair of letters in a string |
Maybe give some examples. The question is ambiguous. But, if I properly understood, would parse string character by character and create a table with counted items.
|
for example cout how many times "ab" apears in "abrtyabvds"
|
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
|
There's really no way to "help" with this that doesn't amount to doing your homework for you.
|
i dont think stroustrup is a mmeber here....and i dont need the problem resolved,just some ideas,i think the function should have 2 string arguments,the string and the pair,but i dont know how to iterate the string so i can search for multiple pairs
|
Quote:
|
I'm not a C++ programmer, so don't know exactly what is possible in plain C++ but I would look for a method that is (more or less) the equivalent of the C strstr() function.
It should return the position of the needle in the haystack so you can use that to calculate an offset for the next iteration. |
I can't take it anymore.
Here's how you would do it in Python: Code:
def pairCount(letterSet, pair): |
Quote:
|
Yes, that's exactly what I was doing.
Now that you've mentioned it, I've noticed that Python's str type has a count() method that can do it in a single statement. But that wouldn't have been nearly as helpful to the OP. |
Quote:
|
Quote:
http://www.cplusplus.com/reference/string/string/ Quote:
is quite a bit more flexible and powerful than strstr(), but I think it is what you are suggesting. It is also what I would suggest for the OP. http://www.cplusplus.com/reference/string/string/find/ In this case, identifying a good function in the documentation is almost "doing the homework", which we're not supposed to do. But I think this is a legitimate reply anyway. C++ homework is supposed to be about reading that documentation and writing the code. It is not supposed to be about the struggle required because you don't yet know easy ways to find things in online documentation. |
What is the expected answer for counting "aa" in the string "aaaaaa"? 3 or 5?
|
All times are GMT -5. The time now is 04:53 PM. |