473,378 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Better implementation of "isPalindromic"

I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}

Jun 30 '06 #1
10 1674
le**********@gmail.com wrote:
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{ return str = string(str.rbegin(),str.rend()); string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}

Jun 30 '06 #2
return equal(str.begin(), str.end(), str.rbegin());

Jun 30 '06 #3
<le**********@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


The current implementation of isPalindrome is doing too much work, as you
seem to have gathered. You don't need to make a reversed copy of the string,
or do (at most) str.length() comparisons. You can get away with (at most)
str.length() / 2 comparisons and no additional space:

bool isPalindrome(const std::string& s)
{
for(int i=0, len=s.length(); i<len/2; ++i)
{
if(s[i] != s[len-1-i]) return false;
}
return true;
}

Hope this helps :)
Stu
Jun 30 '06 #4
Chandu wrote:
return equal(str.begin(), str.end(), str.rbegin());


return equal(str.begin(), str.begin() + str.length()/2, str.rbegin());

Cheers! --M

Jun 30 '06 #5
I would do something like this, to take into account spaces,
punctuation, and capitalization.

bool isPalindrome(const std::string& s)
{
int l = s.length();
int b=0;
int e=l;

do
{
while ( b < l && !isalpha(s[b]) ) b++;
while ( e >= 0 && !isalpha(s[e]) ) e--;
if ( b <= e && tolower(s[b]) != tolower(s[e]) ) return false;
b++; e--;
}
while ( b <= e );

return true;
}

Stuart Golodetz wrote:
<le**********@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


The current implementation of isPalindrome is doing too much work, as you
seem to have gathered. You don't need to make a reversed copy of the string,
or do (at most) str.length() comparisons. You can get away with (at most)
str.length() / 2 comparisons and no additional space:

bool isPalindrome(const std::string& s)
{
for(int i=0, len=s.length(); i<len/2; ++i)
{
if(s[i] != s[len-1-i]) return false;
}
return true;
}

Hope this helps :)
Stu


Jun 30 '06 #6
Grr, I meant
int l = s.length()-1;

sba.nospam.ke...@gmail.com wrote:
I would do something like this, to take into account spaces,
punctuation, and capitalization.

bool isPalindrome(const std::string& s)
{
int l = s.length();
int b=0;
int e=l;

do
{
while ( b < l && !isalpha(s[b]) ) b++;
while ( e >= 0 && !isalpha(s[e]) ) e--;
if ( b <= e && tolower(s[b]) != tolower(s[e]) ) return false;
b++; e--;
}
while ( b <= e );

return true;
}

Stuart Golodetz wrote:
<le**********@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


The current implementation of isPalindrome is doing too much work, as you
seem to have gathered. You don't need to make a reversed copy of the string,
or do (at most) str.length() comparisons. You can get away with (at most)
str.length() / 2 comparisons and no additional space:

bool isPalindrome(const std::string& s)
{
for(int i=0, len=s.length(); i<len/2; ++i)
{
if(s[i] != s[len-1-i]) return false;
}
return true;
}

Hope this helps :)
Stu


Jun 30 '06 #7

sb**************@gmail.com wrote:
I would do something like this, to take into account spaces,
punctuation, and capitalization.

bool isPalindrome(const std::string& s)
{
int l = s.length();
int b=0;
int e=l;

do
{
while ( b < l && !isalpha(s[b]) ) b++;
while ( e >= 0 && !isalpha(s[e]) ) e--;
if ( b <= e && tolower(s[b]) != tolower(s[e]) ) return false;
b++; e--;
}
while ( b <= e );

return true;
}

Stuart Golodetz wrote:
<le**********@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


The current implementation of isPalindrome is doing too much work, as you
seem to have gathered. You don't need to make a reversed copy of the string,
or do (at most) str.length() comparisons. You can get away with (at most)
str.length() / 2 comparisons and no additional space:

bool isPalindrome(const std::string& s)
{
for(int i=0, len=s.length(); i<len/2; ++i)
{
if(s[i] != s[len-1-i]) return false;
}
return true;
}

Hope this helps :)
Stu

Very good implementation.

Jun 30 '06 #8
Very good implementation
sb**************@gmail.com wrote:
I would do something like this, to take into account spaces,
punctuation, and capitalization.

bool isPalindrome(const std::string& s)
{
int l = s.length();
int b=0;
int e=l;

do
{
while ( b < l && !isalpha(s[b]) ) b++;
while ( e >= 0 && !isalpha(s[e]) ) e--;
if ( b <= e && tolower(s[b]) != tolower(s[e]) ) return false;
b++; e--;
}
while ( b <= e );

return true;
}

Stuart Golodetz wrote:
<le**********@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}

bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


The current implementation of isPalindrome is doing too much work, as you
seem to have gathered. You don't need to make a reversed copy of the string,
or do (at most) str.length() comparisons. You can get away with (at most)
str.length() / 2 comparisons and no additional space:

bool isPalindrome(const std::string& s)
{
for(int i=0, len=s.length(); i<len/2; ++i)
{
if(s[i] != s[len-1-i]) return false;
}
return true;
}

Hope this helps :)
Stu


Jun 30 '06 #9
le**********@gmail.com wrote:
I've tried to make a function that takes a string and tells whether
this is Palindromic or not. The code I've written is below. I try to
find ways to optimize the performance of my code. Any suggestions?
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>
using std::string;
using std::cout;
using std::reverse;
void toLower (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
dest+=tolower(*i);
}
void removePunctuation (const string& str,string& dest)
{
string::const_iterator i;
for(i=str.begin();i!=str.end();++i)
if(isalpha(*i)) dest+=*i;
}
bool isPalindrome (string& str)
{
string reversed=str;
reverse(reversed.begin(),reversed.end());
return str==reversed;
}
bool isPalindrome(const string &s)
{
const size_t n = s.size();
for(size_t i = 0; i < n / 2; ++i)
if(s[i] != s[n - i - 1])
return false;
return true;
}

Should give a huge performance improvement for small strings (no memory
allocation required) and strings that are no palindromes.
bool isPalindromic (const string& phrase)
{
string dest0,dest1;
toLower(phrase,dest0);
removePunctuation(dest0,dest1);
return isPalindrome(dest1);
}
int main(int argc, char* argv[])
{
cout<<isPalindromic("anna");
return 0;
}


Jun 30 '06 #10
mlimber wrote:
Chandu wrote:
return equal(str.begin(), str.end(), str.rbegin());


return equal(str.begin(), str.begin() + str.length()/2, str.rbegin());


I really like these implementations, as examples of the utility of STL
algorithms. However, they do not solve the same problem as the
original code. The original code appears to remove punctuation and
spacing, and be case-insensitive.

One way to fix things so that the above code would still work would be
to write an iterator adapter that converts to lower case and discards
non-alphabetic characters.

Luke

Jun 30 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Caroline | last post by:
For the open source community, Can anyone provide a better implementation for the one at http://fire.prohosting.com/vasistas/ ?
85
by: masood.iqbal | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
17
by: Soumyadip Rakshit | last post by:
Hi, Could anyone direct me to a very good 1D DCT Implementation in C/C++ My data is in sets of 8 numbers. Thanks a ton, Soumyadip.
34
by: pamela fluente | last post by:
I would like to hear your *opinion and advice* on best programming practice under .NET. Given that several time we cannot change: MyCollection.Clear into the instantiation of a NEW...
1
by: aine_canby | last post by:
Hi, I have the following simple class: /// <summary> /// Representative of a lab /// </summary> public class Lab { string name;
13
by: Marco Bizzarri | last post by:
Sorry... pressed enter but really didn't want to. As I said, let's say I have a class class A: def __init__(self): self.x = None
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.