Here's my code to find palindrome in string..But surely it's too slow..Is there any good algorithms for such problems..And for some cases it's not giving the right answer.Thanks for the help.
nirbilcahn 0 Newbie Poster
Recommended Answers
Jump to Post#include <string> #include <cctype> #include <algorithm> #include <iostream> bool is_palindrome( const std::string& str ) { std::string s ; for( char c : str ) if( std::isalpha(c) ) s += std::toupper(c) ; return !s.empty() && std::equal( s.begin(), s.begin() + s.size()/2, s.rbegin() ) ; } int main() { const …
Jump to PostYour code won't print all palindromes in all cases:
A palindrome can contain other palindromes that aren't its "inner strings". Take the string "kakykak" for example. Your algorithm will find "kakykak", "akyka" and "kyk", but not the two "kak"s.
Another problem is that palindromes can overlap. For example "akala" contains …
All 7 Replies
Trap910 0 Newbie Poster
nirbilcahn 0 Newbie Poster
vijayan121 1,152 Posting Virtuoso
sepp2k 378 Practically a Master Poster
Labdabeta 182 Posting Pro in Training Featured Poster
sepp2k 378 Practically a Master Poster
iamthwee commented: nice +14
vijayan121 1,152 Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.