Hi there,
I am working on a palindrome program which is here:

#include <iostream>

#include <string>

using namespace std ; 

int main( )

{
	cout << "Insert the word to test: " ;

	string word ;

	string test ;

	int i ;

	int j ;

	cin >> word ;

	

	cout << "The word you input is " << word << " " << endl ;

	test = word ;
	
	for ( i = 0 ,  j = ( word.size() -1 ); i < word.size(), j >= 0 ; i++, j-- )
		{
	
			
			word.at( i ) = word.at( j );
						

		}
cout << "Original Word is " << test << "\n and new word is " << word << endl;
	if (test == word) cout << "Palindromes!";
	else cout<<"wrong!" ;



}

Now, it compiles fine and it even works but this line

cout << "Original Word is " << test << "\n and new word is " << word << endl;

showed me that the program doesn't actually works in a sense because for whatever reason in the process of copying from

word.at( i ) = word.at( j );

the character change...When I compile it the compiler returns 2 warnings, but got no idea what that means...any idea?
thanks

warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
warning C4018: '<' : signed/unsigned mismatch

word.at(i) = test.at(j);
compare to:
word.at( i ) = word.at( j ); // did you really want to write into what you're copying from?

As for your warning:
j = ( word.size() -1 );
j is an int, size() returns a size_t, which is an unsigned int
In this case, you can ignore the warning

the < is for the same reason, i < word.size() (int<unsigned int)
You won't be having any 2.1 billion character words, so you don't have to be concerned. The compiler doesn't know that, so it's warning you.

Edited 6 Years Ago by Unimportant: n/a

Hi, thanks. Well when I said

word.at( i ) = word.at( j );

was because I wanted to copy the reversed word back into the same string, which is why I said

test = word ;

, so to keep my original word and then eventually compared with the reverse

if (test == word)

. I actually did try before posting to do what you suggested but the program doesn't run, it aborts, it says that there is an error even if it compiles fine.
What really bugs me is that if say I input the word "attat" in my program. It works fine in a way, because it doesn't recognise it as a palindrome, but when it gets to

out << "Original Word is " << test << "\n and new word is " << word << endl;

the output is for the original word "attat" - as you would expect - but for the new word is instead "tatat". This happens with every word apart from polindromes...and I can't get why...

I just explained this to you above.

word.at(i) = word.at(j); <-- you are changing the value of word

The reason I changed it to test for you, is because you aren't changing test.
When you output the contents of your test variable, it will be the same as when you set test equal to the value of word.

Basically, "word" is the only thing you are changing.

I also pointed out, that while you are changing it, you're also reading from the changed variable.

word.at(i) = word.at(j)
if i is 0, and j is 4
your word is attat

a = t

the word is now:
tttat

a = t

the word is now
tatat

You changed word.

You didn't reverse anything.

I specifically changed word.at(j) to test.at(j) because test is not being changed.


Edit: Here is a graphical example

std::string word("attat")
word[0] == a;
word[1] == t;
word[2] == t;
word[3] == a;
word[4] == t;

word[0] = word[4]; // <-- what did you expect? Why is it surprising that word changes when you personally change it
word[0] == t; // this now contains whatever character was in word[4]
word[1] == t;
word[2] == t;
word[3] == a;
word[4] == t;

word[0] = test[4];
word[0] == t; // this is the last letter of test, but
test[0] == a; // this is still a, because test remains unchanged

[B]so if your loop reads from TEST and NOT WORD, word will be:[/B]
word[0] == t;
word[1] == a;
word[2] == t;
word[3] == t;
word[4] == a;

after copying all values from test in reverse order.

Edited 6 Years Ago by Unimportant: n/a

ah, yes I see now! So I was trying to copy from word to word rather than from test to word. I wanted to have word changed but I was changing it the wrong way!
thanks

thanks, I spent ages changing my program, debugging it and it was a little silly glitch.
Cheers, now it is clear.

This question has already been answered. Start a new discussion instead.