LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2010, 02:52 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
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
 
Old 09-09-2010, 04:51 PM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
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.
 
Old 09-09-2010, 04:55 PM   #3
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
for example cout how many times "ab" apears in "abrtyabvds"
 
Old 09-09-2010, 05:09 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,669

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
Old 09-09-2010, 05:12 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
There's really no way to "help" with this that doesn't amount to doing your homework for you.
 
Old 09-09-2010, 05:15 PM   #6
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
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
 
Old 09-09-2010, 09:46 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
... 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' ?
 
Old 09-09-2010, 11:34 PM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 09-09-2010, 11:44 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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.

Last edited by dugan; 09-10-2010 at 12:06 AM.
 
Old 09-10-2010, 12:23 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by dugan View Post
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 )
 
Old 09-10-2010, 12:36 AM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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.

Last edited by dugan; 09-10-2010 at 01:03 AM.
 
Old 09-10-2010, 01:04 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by dugan View Post
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.
 
Old 09-10-2010, 08:02 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Wim Sturkenboom View Post
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.

Last edited by johnsfine; 09-10-2010 at 08:11 AM.
 
Old 09-10-2010, 08:03 AM   #14
rriggs
Member
 
Registered: Mar 2009
Location: Colorado, US
Distribution: Fedora 13, Fedora 14, RHEL6 Beta
Posts: 46

Rep: Reputation: 17
What is the expected answer for counting "aa" in the string "aaaaaa"? 3 or 5?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem linking f77 and f90 object files Make problem (I think) TheBrick Programming 1 05-22-2006 06:17 AM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
Lan configuration problem - NFS boot problem - RX&TX packets errors 242VDM242 Linux - Networking 4 11-25-2004 01:35 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration