Stroustrup 5.9 exercise 12

A

arnuld

i am not able to come up with any solution for this. i am not even
able to think of one line of code for this solution.

/* Strouostrup, 5.9, exercise 12
*
*
* STATEMENT:
* write a function that counts the number of occurrences of a pair of
* letters ina "string" and another that does the same in a zero-
terminated
* array fo char(a C style string). e.g. the pair "ab" appears twice in
* "xabaacbaxabb"
*
*/

#include<iostream>
#include<string>

int main()
{
std::string s, pair;
std::cout << "Enter a word: ";
std::cin >> s;

std::cout << "Enter a pair of letters or a single letter you want to
find in the word: ";
std::cin >> pair;

// that's ALL i can think of
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

i am not able to come up with any solution for this. i am not even
able to think of one line of code for this solution.

/* Strouostrup, 5.9, exercise 12
*
*
* STATEMENT:
* write a function that counts the number of occurrences of a pair of
* letters ina "string" and another that does the same in a zero-
terminated
* array fo char(a C style string). e.g. the pair "ab" appears twice in
* "xabaacbaxabb"
*
*/

#include<iostream>
#include<string>

int main()
{
std::string s, pair;
std::cout << "Enter a word: ";
std::cin >> s;

std::cout << "Enter a pair of letters or a single letter you want to
find in the word: ";
std::cin >> pair;

// that's ALL i can think of

You'll want to compare parts of the string, starting with the two
first characters, then the second and third characters and so on. Be
mindful of the fact that you compare with two characters so that you
don't try to compare with the last and the one after the last. So what
you want to do is loop over all the characters from the first to the
second to last.

In the string-case you can use the substr() method to get a part of a
string, or you can use []/at() to get individual characters, the
advantage of using []/at() is that the string and C-string
implementations will be quite similar.

It might be easier to require that you search for a pair of letters
and later, if you still want to, generalize it to work with single
characters or more than a pair.
 
A

arnuld

You'll want to compare parts of the string, starting with the two
first characters, then the second and third characters and so on. Be
mindful of the fact that you compare with two characters so that you
don't try to compare with the last and the one after the last. So what
you want to do is loop over all the characters from the first to the
second to last.

In the string-case you can use the substr() method to get a part of a
string, or you can use []/at() to get individual characters, the
advantage of using []/at() is that the string and C-string
implementations will be quite similar.


i will use "substr()". see down here for my dirty implementation:

It might be easier to require that you search for a pair of letters
and later, if you still want to, generalize it to work with single
characters or more than a pair.

i ahve tried to make it modifiable. i am sure folks here will have
lots of advice for improving my code as it does not look like
Standard C++ code to me :-(




-------- PROGRAMME ---------
/* Strouostrup, 5.9, exercise 12
*
*
* STATEMENT:
* write a function that counts the number of occurrences of a pair of
* letters ina "string" and another that does the same in a zero-
terminated
* array fo char(a C style string). e.g. the pair "ab" appears twice in
* "xabaacbaxabb"
*
*/

#include<iostream>
#include<string>

int main()
{
std::string s, pair;
std::cout << "Enter a word: ";
std::cin >> s;

std::cout << "Enter a pair of letters or a single letter you want to
find in the word: ";
std::cin >> pair;

const int ps = pair.size();
const int ss = s.size();
int matches = 0;

if(ps < ss)
{
for(int i=0; i < ss; ++i)
if(pair == s.substr(i,ps))
++matches;
}

std::cout << "you entered: \t"
<< s
<< '\n'
<< "letter(s) to match: "
<< pair
<< '\n'
<< "Matches are: "
<< matches
<< std::endl;

return 0;
}

--------- OUTPUT -----------

[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O ex_5-12.cpp
[arch@voodo tc++pl]$ ./a.out
Enter a word: xabaacbaxabb
Enter a pair of letters or a single letter you want to find in the
word: ab
you entered: xabaacbaxabb
letter(s) to match: ab
Matches are: 2

[arch@voodo tc++pl]$ ./a.out
Enter a word: likeallthatsheshrshte
Enter a pair of letters or a single letter you want to find in the
word: she
you entered: likeallthatsheshrshte
letter(s) to match: she
Matches are: 1
 
D

Daniel T.

arnuld said:
i am not able to come up with any solution for this. i am not even
able to think of one line of code for this solution.

/* Strouostrup, 5.9, exercise 12
*
*
* STATEMENT:
* write a function that counts the number of occurrences of a pair of
* letters ina "string" and another that does the same in a zero-
terminated
* array fo char(a C style string). e.g. the pair "ab" appears twice in
* "xabaacbaxabb"
*
*/

#include<iostream>
#include<string>

int main()
{
std::string s, pair;
std::cout << "Enter a word: ";
std::cin >> s;

std::cout << "Enter a pair of letters or a single letter you want to
find in the word: ";
std::cin >> pair;

// that's ALL i can think of

1) search for the first character in the pair,
2) check if the character after it is the second one of the pair,
3) search for the next character of the pair.
4) goto 2
 
D

Daniel T.

arnuld said:
You'll want to compare parts of the string, starting with the two
first characters, then the second and third characters and so on. Be
mindful of the fact that you compare with two characters so that you
don't try to compare with the last and the one after the last. So what
you want to do is loop over all the characters from the first to the
second to last.

In the string-case you can use the substr() method to get a part of a
string, or you can use []/at() to get individual characters, the
advantage of using []/at() is that the string and C-string
implementations will be quite similar.


i will use "substr()". see down here for my dirty implementation:

It might be easier to require that you search for a pair of letters
and later, if you still want to, generalize it to work with single
characters or more than a pair.

i ahve tried to make it modifiable. i am sure folks here will have
lots of advice for improving my code as it does not look like
Standard C++ code to me :-(

Your code is fine, but doing more work than it needs to. It can be
simplified quite a bit. I.E., the only problem I see is that you don't
yet know what the standard library already has available.
-------- PROGRAMME ---------
/* Strouostrup, 5.9, exercise 12
*
*
* STATEMENT:
* write a function that counts the number of occurrences of a pair of
* letters ina "string" and another that does the same in a zero-
terminated
* array fo char(a C style string). e.g. the pair "ab" appears twice in
* "xabaacbaxabb"
*
*/

#include<iostream>
#include<string>

int main()
{
std::string s, pair;
std::cout << "Enter a word: ";
std::cin >> s;

std::cout << "Enter a pair of letters or a single letter you want to
find in the word: ";
std::cin >> pair;

const int ps = pair.size();
const int ss = s.size();
int matches = 0;

if(ps < ss)
{
for(int i=0; i < ss; ++i)
if(pair == s.substr(i,ps))
++matches;
}

Look up std::string::find...

http://www.dinkumware.com/manuals/?manual=compleat&page=string2.html#basi
c_string::find
std::cout << "you entered: \t"
<< s
<< '\n'
<< "letter(s) to match: "
<< pair
<< '\n'
<< "Matches are: "
<< matches
<< std::endl;

return 0;
}

One last comment. It's time to start putting code into separate
functions:

int countOccurrences( const std::string& source, const std::string& pair
)
{
// do the job here
// return the result
}

int main()
{
// get the data
// call 'countOccurrences' using the data
// put the results
}
 
J

James Kanze

1) search for the first character in the pair,
2) check if the character after it is the second one of the pair,
3) search for the next character of the pair.
4) goto 2

Why 3?

This is basically just searching for a substring or a pattern;
the fact that the search string is always two characters is
really irrelevant. There are many algorithms for doing this
more or less rapidly over a very large string, but the classic
solution uses nested tests; you write a function which says
whether you have a match at the current position, call it, then
advance. Searching for the first character separately may be a
valid optimization, if searching for a single character is
particularly rapid. Searching for the last character can be
even faster, because depending on the character that you
actually find there, you may be able to jump more than a single
character ahead (BM search). But I rather think that that's
getting ahead of the original poster.

Of course, in modern C++, you don't write such things, because
there are already standard library functions available which do
it for you. I *don't* think it's the goal of the exercises, and
as a learning experience, I think that the original poster
should write the code without using such functions, but he might
also want to look into std::string::find or std::search.

And while I'm at it, I might note that the specification is
ambiguous. How many times does "aa" occur in "baaab"? Before
I'd write one line of code, I'd clarify this point. For the
exercise, it really doesn't matter. But it's important to write
code against a rigorous requirements specification, so that you
can know whether it works or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top