Created
August 17, 2012 04:43
-
-
Save ZachMassia/3375991 to your computer and use it in GitHub Desktop.
Accelerated C++ Exercise 5-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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