Thread: String comparision

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    Exclamation String comparision

    I'm trying to compare if a string is the same forwards as backwards (for example bob is the same backwards as forwards etc)

    I managed to get the string to reverse and store the reverse string in another string variable

    However when I try to compare the two I never get the right answer, its always stuck on a false answer.

    Code:
    line = "bob";
    
    
    	int len = line.length();
    	for (int i=0; i<len; i++)
    	{
    		if (isalnum(line[i]))
    		{
    		    nospc = nospc + line [i];
    			
    		}
    	
    	}
    	
    	cleaned = nospc;
    	int len2 = nospc.length();
    	for (int h=0; h<len2; h++)
    	{
    		cleaned[h] = tolower(cleaned[h]);
    	}
    	
    	int len3 = cleaned.length();
    	for (int x=len3; x>=0; x--)
    	{
    		lincomp = lincomp + cleaned[x];
    	}
    	isPali = !cleaned.compare (lincomp);
    thats the code I'm using, I've also tried the == deal with no success.

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> for (int x=len3; x>=0; x--)

    len3 is not a valid index into an array (or string in this case). You need to start at len3 - 1.

    You should go ahead and use == once you fix that problem, it is clearer. You can also use str += ch instead of str = str + ch.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    Thank you so much, that fixed everything!

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    try it with some standard library algorithms
    usestd::string and reverse_copy .

    you'll be amazed how easy it is!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Another way is to use the fact that std::string has built-in reverse iterators, which you can pass to the constructor, eg,

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string original("hello");
        string backwards(original.rbegin(), original.rend());
        cout << original << " " << backwards << endl;
        cin.get();
    }

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by ChaosEngine
    try it with some standard library algorithms
    usestd::string and reverse_copy .

    you'll be amazed how easy it is!
    But you don't learn how to manipulate a string....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by WaltP
    But you don't learn how to manipulate a string....
    yes you do, you just don't learn how to manipulate an array of characters!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    using namespace std;
    
    ...
    
    string str = "bob";
    
    if( equal(str.begin(),str.begin()+str.length()/2,str.rbegin()) )
        cout << "Equal!" << endl;
    else
        cout << "Not equal!" << endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    lol. The master! Minimum number of comparisons noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM