Skip to content

Instantly share code, notes, and snippets.

@ZachMassia
Created August 17, 2012 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZachMassia/3375991 to your computer and use it in GitHub Desktop.
Save ZachMassia/3375991 to your computer and use it in GitHub Desktop.
Accelerated C++ Exercise 5-10
int ExerciseRunner::ex5_10()
{
// Write a program to find all the palindromes in a dictionary.
// Next find the longest one.
typedef std::vector<std::string> str_vec;
str_vec palindromes;
str_vec dict = {"civic", // x
"hello",
"zachary"
"deleveled",// x
"racecar", // x
"john"
"radar", // x
"refer"}; // x
for (auto it : dict)
{
std::string backwards;
for (auto r_it = it.crbegin(); r_it != it.crend(); ++r_it)
{
backwards.push_back(*r_it);
}
if (it == backwards)
palindromes.push_back(it);
}
std::string longest = palindromes[0];
std::cout << " -- Exercise 5-10 -- \n\n" << "Palindromes:\n";
for (auto it : palindromes)
{
std::cout << "\t" << it << "\n";
if (it.length() > longest.length())
longest = it;
}
std::cout << "\nLongest palindrome:\n\t" << longest << std::endl;
// -- End of Method -- //
return 0;
}
** Output **
-- Exercise 5-10 --
Palindromes:
civic
racecar
refer
Longest palindrome:
racecar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment