This is my answer to problem 5-10 from Accelerated C++. I'd appreciate
any comments (regarding style, efficiency, standards, etc) you fellows
have.
The resulting program seems reasonably fast, searching through a
dictionary of 300,000+ words in 3 seconds on my PowerBook G4.
// Write a program that finds all the palindromes in a dictionary,
// and also find the largest palindrome.
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::string; using std::endl;
using std::vector;
bool is_palindrome(c onst string&);
bool compare_word_le ngth(const string &, const string &);
int main()
{
cout << "This program will find all the palindromes in a dictionary "
"and will find the largest one." << std::endl;
string input;
vector<string> palindromes;
// Get the palindromes
while (cin >> input)
if (is_palindrome( input))
palindromes.pus h_back(input);
// Display the palindromes
cout << "\nThe palindromes were:\n";
vector<string>: :const_iterator iter;
for (iter = palindromes.beg in(); iter != palindromes.end (); ++iter)
{
cout << *iter << '\t';
}
cout << '\n';
// Sorting palindromes
sort (palindromes.be gin(), palindromes.end (), compare_word_le ngth);
// Display the shortest and longest one
string smallest = *palindromes.be gin();
string largest = *(palindromes.e nd()-1);
cout << "\nThe shortest palindrome was '" << smallest << "'"
" and the longest one was '" << largest << "'\n";
cout << endl;
return 0;
}
bool is_palindrome(c onst string &input)
{
string::const_i terator begin, end;
begin = input.begin();
end = input.end() - 1;
while (begin < end)
{
if (*begin != *end)
return false;
begin++;
end--;
}
return true;
}
bool compare_word_le ngth(const string &a, const string &b)
{
return (a.size() < b.size());
}
any comments (regarding style, efficiency, standards, etc) you fellows
have.
The resulting program seems reasonably fast, searching through a
dictionary of 300,000+ words in 3 seconds on my PowerBook G4.
// Write a program that finds all the palindromes in a dictionary,
// and also find the largest palindrome.
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::string; using std::endl;
using std::vector;
bool is_palindrome(c onst string&);
bool compare_word_le ngth(const string &, const string &);
int main()
{
cout << "This program will find all the palindromes in a dictionary "
"and will find the largest one." << std::endl;
string input;
vector<string> palindromes;
// Get the palindromes
while (cin >> input)
if (is_palindrome( input))
palindromes.pus h_back(input);
// Display the palindromes
cout << "\nThe palindromes were:\n";
vector<string>: :const_iterator iter;
for (iter = palindromes.beg in(); iter != palindromes.end (); ++iter)
{
cout << *iter << '\t';
}
cout << '\n';
// Sorting palindromes
sort (palindromes.be gin(), palindromes.end (), compare_word_le ngth);
// Display the shortest and longest one
string smallest = *palindromes.be gin();
string largest = *(palindromes.e nd()-1);
cout << "\nThe shortest palindrome was '" << smallest << "'"
" and the longest one was '" << largest << "'\n";
cout << endl;
return 0;
}
bool is_palindrome(c onst string &input)
{
string::const_i terator begin, end;
begin = input.begin();
end = input.end() - 1;
while (begin < end)
{
if (*begin != *end)
return false;
begin++;
end--;
}
return true;
}
bool compare_word_le ngth(const string &a, const string &b)
{
return (a.size() < b.size());
}
Comment