Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


c++ iterator help

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
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

PostPosted: Mon Dec 05, 2011 5:39 pm    Post subject: c++ iterator help Reply with quote

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
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Dec 05, 2011 6:58 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AverageAzn247
Grandmaster Cheater
Reputation: 34

Joined: 01 Oct 2007
Posts: 909
Location: Austin,TX with 72 virgins

PostPosted: Mon Dec 05, 2011 7:25 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Dec 05, 2011 8:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AverageAzn247
Grandmaster Cheater
Reputation: 34

Joined: 01 Oct 2007
Posts: 909
Location: Austin,TX with 72 virgins

PostPosted: Mon Dec 05, 2011 11:12 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Dec 06, 2011 7:04 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AverageAzn247
Grandmaster Cheater
Reputation: 34

Joined: 01 Oct 2007
Posts: 909
Location: Austin,TX with 72 virgins

PostPosted: Wed Dec 07, 2011 1:38 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Dec 07, 2011 1:48 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AverageAzn247
Grandmaster Cheater
Reputation: 34

Joined: 01 Oct 2007
Posts: 909
Location: Austin,TX with 72 virgins

PostPosted: Wed Dec 07, 2011 2:32 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites