Hello all, I am not taking a class or asking for help with homework. I am attempting to learn C++ on my own and I do not understand the logic within one of the sample programs within my book.

#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter a word: ";
string word;
cin >> word;

//physically modify string object
char temp;
int i, j;
for (j = 0, i = word.size() - 1; j < i; --i, ++j)
{
temp = word[i];
word[i] = word[j];
word[j] = temp;
}                //end block
cout << word << "\nDone\n";
return 0;
}

Here is the logic part that I don't understand. Within the for loop when you set word = word[j], aren't you setting the first letter to the last letter and then inserting it into temp as well? In the sample run of the program they enter parts and the end result is strap. The program is supposed to spell the word backwards but with the code why isn't the the last letter of the word entered into temp as a result of temp = word and next the first letter being inserted as a result of word[j] = temp? It almost seems to me that the word[j] = temp; is not needed. If anyone can offer insight I would greatly appreciate it.

Recommended Answers

All 10 Replies

Don't use spaces between in the code tags for them to work.

That code is swapping the variables word and word[j]. Temp is needed to do the switch (at least the way it's being done here).

Say you have a bucket A full of orange juice and bucket B full of water. To swap them you'd need a third bucket named temp. First, pore the oj in A into temp. Now A is empty (well, not in the program, but just roll with it), and temp has oj. Then, pore the water from B into A. A has water, B is empty, and temp has the oj. Now, dump the oj from temp into B. There. You've swapped them.

Hope this helped a bit.

A slightly more direct explanation:

temp = word[i];        // Save the 1st letter
word[i] = word[j];     // put the 2nd letter where the 1st was
word[j] = temp;        // put the 1st letter where the 2nd was

And please learn to format your code. It's much easier for you (and us) to read. Poorly formatted code does tend to make people that could help you just skip your code.

i               j
+---+---+---+---+---+
| p | a | r | t | s |
+---+---+---+---+---+

Draw it on paper, then run the code by hand.
Add another variable to the paper called temp.

A slightly more direct explanation:

temp = word[i];        // Save the 1st letter
word[i] = word[j];     // put the 2nd letter where the 1st was
word[j] = temp;        // put the 1st letter where the 2nd was

And please learn to format your code. It's much easier for you (and us) to read. Poorly formatted code does tend to make people that could help you just skip your code.

I want to thank everyone for taking the time to reply to me, I really appreciate it. I get it now. Thank you very much!

i guess xor swapping could do here too, no need for another variable!
slightly fancier code:

for (j = 0, i = word.size() - 1; j < i; --i, ++j)
{
word[i] ^= word[j];
word[j] ^= word[i];
word[i] ^= word[j];
}

or even simplier

for (j = 0, i = word.size() - 1; j < i; --i, ++j)
word[i] ^= ( word[j] ^= ( word[i] ^= word[j] ) );

i guess xor swapping could do here too, no need for another variable!
slightly fancier code:

for (j = 0, i = word.size() - 1; j < i; --i, ++j)
{
word[i] ^= word[j];
word[j] ^= word[i];
word[i] ^= word[j];
}

or even simplier

for (j = 0, i = word.size() - 1; j < i; --i, ++j)
word[i] ^= ( word[j] ^= ( word[i] ^= word[j] ) );

yeah, the author says that there is a better way to do it, but he hasn't introduced them yet. I'm only on chapter 5: Loops and Relational Expressions. I don't even know which operator ^= is.

even so...if i'd cast everything to char it would work, wouldn't it?
damn i like xor too much
//edit
oh, i've read the answer... shouldn't there be a standard for that stuff..?
cause you can always find a bugged compiler that can't compile something right.

Or just plain WRONG!
http://c-faq.com/expr/xorswapexpr.html

You're using a compiler, not a programmable calculator which literally interprets every character of your source code.

Overly cute statements like this end up making things worse, not better.

Not only that, but the bit-shifting operators will only swap values properly for primitives like ints and chars... what about swapping objects? Do you really feel like defining operator overloads for swapping objects in a similar manner?

Stick with 3 data types, or if you absolutely must you can use a ternary swap

Note: example is written in java. It's just for a general concept on how you could get away with using two storage datatypes, even though its not really recommended--

static <T extends Comparable<T> > void sortArrayList(ArrayList<T> arg, boolean order) throws Exception{
		int turn = 0;
		while(!Sort_Kit.<T>isSorted(arg)){
			for(int i = 0; i < arg.size() - 1; i++){
				T temp = (order)
				? (arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i))
				? (arg.get(i)) : (arg.get(i + 1)) ) : ((arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i)) )
				? (arg.get(i)) : (arg.get(i + 1))),
				 temp2 = (order)
				? (arg.get(i).compareTo(arg.get(i + 1)) >  arg.get(i + 1).compareTo(arg.get(i))
				? (arg.get(i)) : (arg.get(i + 1)) ) : ((arg.get(i).compareTo(arg.get(i + 1)) <  arg.get(i + 1).compareTo(arg.get(i)) )
				? (arg.get(i)) : (arg.get(i + 1)));
				arg.set(i, temp);
				arg.set(i + 1, temp2);
				System.out.println(temp + "  " + temp2);
				Thread.sleep(250); // for debug purposes
			}
			System.out.println(arg);
			turn++;
		}
        passes = turn;
	}

i understand i cannot use xorswap on objects, well that is obvious. anyway, i'm here to learn too so i appreciate all helpful advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.