I can’t begin to express how delighted I am at all of the wonderful comments, suggestions, and corrections that have been coming in on the Accelerated C++ Solutions. I would be updating the pages and replying to the comments more, but I’m horribly swamped at my day job right now and I barely have time to do anything but camp out at the office.
Please, keep the comments coming, and I’ll do my best to give them the attention they deserve in a few weeks! Thank you all so much!
Share
5-11.
I really hope the code tags work in html, or I’m gonna be lookin’ stupid.
Our friend read_words is back!
#include
#include
#include
#include "read_words.h"
using std::vector; using std::endl; using std::istream;
using std::string;
istream& read_words(istream& in, vector& words)
{
if (in)
{
words.clear();
string word;
while (in >> word)
{
words.push_back(word);
}
in.clear();
}
return in;
}
I did a little extra with the function to find ascenders and descenders. The only function you actually need is the one at the bottom “ascende_descende”, but I figured why not add functions to find them separately in case that was ever needed
#include
#include
#include "ascenders_descenders.h"
using std::string; using std::vector;
// Ascenders are parts of lowercase letters that extend above the text line
// Find and report whether a word contains any Ascenders
// Ascenders: b, d, f, h, k, l, and t
bool ascenders(string s)
{
string ascende = "bdfhklt";
// Compare each letter in the current word to each ascender
// if a match is found, return true
for (string::const_iterator it = s.begin(); it != s.end(); ++it)
{
for (string::const_iterator iter = ascende.begin();
iter < ascende.end(); ++iter)
{
if (*it == *iter)
return true;
} // end for()
} // end for()
return false;
} // end ascenders()
// Descenders are parts of lowercase letters that descend below the text line
// Find and report whether a word contains any Descenders
// Descenders: g, j, p, q, and y
bool descenders(string s)
{
string descende = "gjpqy";
// Compare each letter in the current word to each descender
// if a match is found, return true
for (string::const_iterator it = s.begin(); it != s.end(); ++it)
{
for (string::const_iterator iter = descende.begin();
iter < descende.end(); ++iter)
{
if (*it == *iter)
return true;
} // end for()
} // end for()
return false;
} // end descenders()
// Ascenders are parts of lower case letters that extend above the text line
// Descenders are parts of lower case letters that descend below the text line
// Find and report whether a word contains either Ascenders or Descenders
// Ascenders: b, d, f, h, k, l, and t
// Descenders: g, j, p, q, and y
bool ascende_descende(string s)
{
string a_d = "bdfhkltgjpqy";
// Compare each letter in the current word to each ascender and descender
// if a match is found, return true
for (string::const_iterator it = s.begin(); it != s.end(); ++it)
{
for (string::const_iterator iter = a_d.begin();
iter < a_d.end(); ++iter)
{
if (*it == *iter)
return true;
} // end for()
} // end for()
return false;
} // end ascende_descende()
And lastly, main() which is only using the ascende_decende() function
#include
#include
#include
#include "ascenders_descenders.h"
#include "read_words.h"
using std::cout; using std::endl; using std::string;
using std::vector; using std::cin;
int main(void)
{
cout << "Input desired words, followed by end-of-file. The program will "
<< "then locate and " << endl << "print words with \"ascenders\" "
<< "or \"descenders\": ";
vector words;
while (words.empty())
read_words(cin, words);
string::size_type maxlen = 0;
string longest;
// Format output
cout << endl << "These words contain at least one "
<< "\"ascender\" or \"descender\""
<< endl << string(58, '-') << endl;
// Check for ascenders and descenders and output if either is true
// if both are false, check to see if the current word is longer than
// maxlen, and if it is, set maxlen to the size of the word and insert
// the word into the variable longest
for (vector::const_iterator it = words.begin();
it != words.end(); ++it)
{
if (ascende_descende(*it))
cout << *it <length() > maxlen)
{
longest = *it;
maxlen = it->length();
} // end if...else
} // end for()
// Output the longest word found that is not an ascender or descender
cout << endl << "The longest word containing no "
<< "\"ascenders\" or \"descenders\" is: "
<< longest;
return 0;
} // end program
And an example run
Input desired words, followed by end-of-file. The program will then locate and
print words with "ascenders" or "descenders": output
nope
no
zoos
roar
rawr
konichiwa
These words contain at least one "ascender" or "descender"
----------------------------------------------------------
output
nope
konichiwa
The longest word containing no "ascenders" or "descenders" is: zoos
Hopefully I covered all my bases with the html this time!
5-10
#include <istream>
#include <string>
#include <vector>
#include “read_words.h”
using std::vector; using std::endl; using std::istream;
using std::string;
istream& read_words(istream& in, vector<string>& words)
{
if (in)
{
words.clear();
string word;
while (in >> word)
{
words.push_back(word);
}
in.clear();
}
return in;
}
The function to determine if the word is a palindrome
#include <string>
#include “palindrome.h”
using std::string;
bool palindrome(string s)
{
// Compare the first and last letter. Then increment i and decrement j
// to compare the second and second to last letter. Repeat until i != j
// or until a palindrome is confirmed
for (string::const_iterator i = s.begin(), j = s.end() – 1; i < j; ++i, –j)
{
if (*i != *j)
return false;
}
return true;
}
And main()!
#include <iostream>
#include <string>
#include <vector>
#include “palindrome.h”
#include “read_words.h”
using std::cout; using std::cin; using std::endl;
using std::vector; using std::string;
int main(void)
{
cout << "Enter the list of words you wish to check for palindromes, "
<< endl << "followed by end-of-file: ";
vector<string> words;
while (words.empty())
read_words(cin, words);
string::size_type maxlen = 0;
string longest;
cout << endl << endl;
// Start at the first word in the vector and output each palindrome
// immediately after they have been found
for (vector<string>::const_iterator it = words.begin();
it != words.end(); ++it)
{
if (palindrome(*it))
{
cout << *it <length() > maxlen)
{
maxlen = it->length();
longest = *it;
} // end inner if
} // end outer if
} // end for
cout << "The longest palindrome is: " << longest << endl;
return 0;
}
Hello,
I tried to access your solution page, but was not able to do so since I got “The page cannot be found” error message.
Could you let me know what went wrong and when I can access it?
Thank you.
I’m very sorry for the problem. I’ve upgraded the version of WordPress I use on the site, and as a result all permalinks have been removed. You may reach the solutions through the menu link at the top, or through this link:
http://www.parkscomputing.com/?page_id=414
I’m working on a solution currently, and I’m so sorry for the disruption.
Thanks,
PMP