View previous topic :: View next topic |
Author |
Message |
AverageAzn247 Grandmaster Cheater
Reputation: 34
Joined: 01 Oct 2007 Posts: 909 Location: Austin,TX with 72 virgins
|
Posted: Mon Dec 05, 2011 5:39 pm Post subject: c++ iterator help |
|
|
this is supposed to detect if it was a palindrome or not. Anyone know how i can fix it? Trying to return false if it fails to meet the conditions.
bool is_pal(string input)
{
bool check=true;
string::iterator first= input.begin();
string::reverse_iterator last=input.rbegin();
while( *(first++) == *(last++) && first != input.end() )
{
check = false;
}
return check;
}
_________________
Waxxup wrote: | What are Night Elves?
A girl group? |
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Dec 05, 2011 6:58 pm Post subject: |
|
|
Something like this should work:
Code: |
bool is_pal(string input)
{
std::string::iterator first = input.begin();
std::string::iterator last = input.end();
std::string::reverse_iterator rlast = input.rbegin();
for( first; first != last; ++first, ++rlast )
{
if( *first != *rlast )
return false;
}
return true;
}
|
Assuming you are just doing single words like 'racecar' without punctuation and such.
_________________
- Retired. |
|
Back to top |
|
 |
AverageAzn247 Grandmaster Cheater
Reputation: 34
Joined: 01 Oct 2007 Posts: 909 Location: Austin,TX with 72 virgins
|
Posted: Mon Dec 05, 2011 7:25 pm Post subject: |
|
|
Wiccaan wrote: | Something like this should work:
Code: |
bool is_pal(string input)
{
std::string::iterator first = input.begin();
std::string::iterator last = input.end();
std::string::reverse_iterator rlast = input.rbegin();
for( first; first != last; ++first, ++rlast )
{
if( *first != *rlast )
return false;
}
return true;
}
|
Assuming you are just doing single words like 'racecar' without punctuation and such. | i am trying to get it to read a sentence ex "Madam, I'm Adam.". I already removed the punc and white spaces before sending it into is_pal
http://www.mediafire.com/?tstajs5o6arwsoi
_________________
Waxxup wrote: | What are Night Elves?
A girl group? |
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Dec 05, 2011 8:04 pm Post subject: |
|
|
You need to convert the text to lowercase as well before you compare it.
Code: |
bool is_pal( const std::string& str )
{
// Convert input to lower-case.
std::string input = str;
std::transform( input.begin(), input.end(), input.begin(), tolower );
std::string::iterator first = input.begin();
std::string::iterator last = input.end();
std::string::reverse_iterator rlast = input.rbegin();
for( first; first != last; ++first, ++rlast )
{
if( *first != *rlast )
return false;
}
return true;
}
|
Edit : Also note this needs another include:
#include <algorithm>
_________________
- Retired. |
|
Back to top |
|
 |
AverageAzn247 Grandmaster Cheater
Reputation: 34
Joined: 01 Oct 2007 Posts: 909 Location: Austin,TX with 72 virgins
|
Posted: Mon Dec 05, 2011 11:12 pm Post subject: |
|
|
Wiccaan wrote: | You need to convert the text to lowercase as well before you compare it.
Code: |
bool is_pal( const std::string& str )
{
// Convert input to lower-case.
std::string input = str;
std::transform( input.begin(), input.end(), input.begin(), tolower );
std::string::iterator first = input.begin();
std::string::iterator last = input.end();
std::string::reverse_iterator rlast = input.rbegin();
for( first; first != last; ++first, ++rlast )
{
if( *first != *rlast )
return false;
}
return true;
}
|
Edit : Also note this needs another include:
#include <algorithm> | i don't think thats the problem because i already had this
Code: | for(int i=0;i<input.length();i++)
{input[i]=tolower(input[i]);} |
_________________
Waxxup wrote: | What are Night Elves?
A girl group? |
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Dec 06, 2011 7:04 am Post subject: |
|
|
My implementation of this works with the sentence and word versions based on your original post:
Code: |
std::string remove_whitespace( const std::string& str )
{
std::string result;
for( std::string::const_iterator iter = str.begin(), iterend = str.end(); iter != iterend; ++iter )
{
if( *iter != ' ' )
result += *iter;
}
return result;
}
std::string remove_punc( const std::string& str )
{
std::string result;
for( std::string::const_iterator iter = str.begin(), iterend = str.end(); iter != iterend; ++iter )
{
if( ispunct( *iter ) )
result += ' ';
else
result += *iter;
}
return result;
}
bool is_pal( const std::string& str )
{
// Convert input to lower-case.
std::string input = str;
std::transform( input.begin(), input.end(), input.begin(), tolower );
std::string::iterator first = input.begin();
std::string::iterator last = input.end();
std::string::reverse_iterator rlast = input.rbegin();
for( first; first != last; ++first, ++rlast )
{
if( *first != *rlast )
return false;
}
return true;
}
|
Usage:
Code: |
std::string test = remove_punc( std::string( "Madam, I'm Adam." ) );
test = remove_whitespace( test );
bool bTest1 = is_pal( test );
bool bTest2 = is_pal( std::string( "racecar" ) );
bool bTest3 = is_pal( std::string( "herpderp" ) );
|
Returns:
true
true
false
So looks fine on me end.
_________________
- Retired. |
|
Back to top |
|
 |
AverageAzn247 Grandmaster Cheater
Reputation: 34
Joined: 01 Oct 2007 Posts: 909 Location: Austin,TX with 72 virgins
|
Posted: Wed Dec 07, 2011 1:38 pm Post subject: |
|
|
its having problems with white spacing.
Rise to vote, sir.
A Toyota.
are all giving me problems
_________________
Waxxup wrote: | What are Night Elves?
A girl group? |
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Dec 07, 2011 1:48 pm Post subject: |
|
|
AverageAzn247 wrote: | its having problems with white spacing.
Rise to vote, sir.
A Toyota.
are all giving me problems |
Be sure you are calling remove_punc and remove_whitespace before you call is_pal in my example above.
Both the two new examples worked fine for me.
_________________
- Retired. |
|
Back to top |
|
 |
AverageAzn247 Grandmaster Cheater
Reputation: 34
Joined: 01 Oct 2007 Posts: 909 Location: Austin,TX with 72 virgins
|
Posted: Wed Dec 07, 2011 2:32 pm Post subject: |
|
|
my bad. turns out i was sending the the unedited file to the function
_________________
Waxxup wrote: | What are Night Elves?
A girl group? |
|
|
Back to top |
|
 |
|