LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem help c++ (https://www.linuxquestions.org/questions/programming-9/problem-help-c-831249/)

vbx_wx 09-09-2010 02:52 PM

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

eSelix 09-09-2010 04:51 PM

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.

vbx_wx 09-09-2010 04:55 PM

for example cout how many times "ab" apears in "abrtyabvds"

michaelk 09-09-2010 05:09 PM

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.

dugan 09-09-2010 05:12 PM

There's really no way to "help" with this that doesn't amount to doing your homework for you.

vbx_wx 09-09-2010 05:15 PM

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

Sergei Steshenko 09-09-2010 09:46 PM

Quote:

Originally Posted by vbx_wx (Post 4092901)
... i dont know how to iterate the string so i can search for multiple pairs

Start from a simple task - do you know how to iterate over a string looking for letter 'a' ?

Wim Sturkenboom 09-09-2010 11:34 PM

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.

dugan 09-09-2010 11:44 PM

I can't take it anymore.

Here's how you would do it in Python:

Code:

def pairCount(letterSet, pair):
    count = 0
    for i in xrange(len(letterSet) - 1):
        if letterSet[i] == pair[0] and letterSet[i + 1] == pair[1]:
            count = count + 1
    return count;

I thought long and hard about whether I should provide this, but sometimes you just need to see how it's done.

ghostdog74 09-10-2010 12:23 AM

Quote:

Originally Posted by dugan (Post 4093091)
I can't take it anymore.

Here's how you would do it in Python:

Code:

def pairCount(letterSet, pair):
    count = 0
    for i in xrange(len(letterSet) - 1):
        if letterSet[i] == pair[0] and letterSet[i + 1] == pair[1]:
            count = count + 1
    return count;

I thought long and hard about whether I should provide this, but sometimes you just need to see how it's done.

i take it that you are mimicking how its done in C++ and not actually how you do it with Python. (Because it can be done even more simply in Python )

dugan 09-10-2010 12:36 AM

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.

ghostdog74 09-10-2010 01:04 AM

Quote:

Originally Posted by dugan (Post 4093109)
Now that you mention it, I noticed that Python's str type has a count() method that can do it in a single statement. That wouldn't have been nearly as helpful to the OP though.

In school, we learn about nitty gritty details. In real life, we don't really care. As long as someone has done the nitty gritty details and make life easy, we use it. :)

johnsfine 09-10-2010 08:02 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 4093078)
I'm not a C++ programmer, so don't know exactly what is possible in plain C++

What is possible in C++ is big question. But the OP just seems to need the member functions of std::string. That's easy:
http://www.cplusplus.com/reference/string/string/

Quote:

I would look for a method that is (more or less) the equivalent of the C strstr() function.
std::string::find
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.

rriggs 09-10-2010 08:03 AM

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.