Re: [Java] making the text backwards - need help on project
Here's another obvious way, in C++ :wink:
PHP Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string str("abcdefghijklmnopqrstuvwxyz");
int sz = str.size();
for (int i = 2 - sz; i < sz - 1; i++)
{
int a(sz - abs(i) - 1);
int b(sz - abs(i) - 2);
str[a] ^= str[b];
str[b] ^= str[a];
str[a] ^= str[b];
}
cout << str << endl;
return 0;
}
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
Negata
PHP Code:
str[a] ^= str[b];
str[b] ^= str[a];
str[a] ^= str[b];
XOR, wat?
PHP Code:
#include <stdio.h>
#include <string>
int main(int argc, char *argv[])
{
char *szInput = (char *)malloc (512);
char *szOutput = (char *)malloc (512);
memset (szOutput, 0, 512);
scanf ("%[^\n]", szInput);
for (int i = strlen(szInput); i >= 1; --i) szOutput[i] = szInput[i];
printf ("Original: %s\nReversed: %s\n", szInput, szOutput);
free (szOutput);
free (szInput);
return 0;
}
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
Theoretical
XOR, wat?
He's trying to be clever. It's a way to exchange 2 variables without using a temporary variable.
Ok, let's review:
A ^ B = C
C ^ A = B
C ^ B = A
Now, two variables X and Y, we want to exchange them using XOR:
X = X ^ Y = Z (just as a reminder that it's the xor product)
Now, X is the Z value here, now we want to move X into Y:
Y = Y ^ Z = X
Now, X = Z and Y = X, one final move to make X be our Y value:
X = Z ^ Y = Z ^ X = Y
So now our two variables have been exchanged. It's a bit confusing calling it Z^Y on the last step because Y means X, it's notational, I didn't care to do it properly by denoting the original values as X_0 and Y_0 (primarily because subscripts are a pain in the ass to do in vBulletin without a math addon).
Re: [Java] making the text backwards - need help on project
Quote:
Originally Posted by
jMerliN
He's trying to be clever. It's a way to exchange 2 variables without using a temporary variable.
Ok, let's review:
A ^ B = C
C ^ A = B
C ^ B = A
Now, two variables X and Y, we want to exchange them using XOR:
X = X ^ Y = Z (just as a reminder that it's the xor product)
Now, X is the Z value here, now we want to move X into Y:
Y = Y ^ Z = X
Now, X = Z and Y = X, one final move to make X be our Y value:
X = Z ^ Y = Z ^ X = Y
So now our two variables have been exchanged. It's a bit confusing calling it Z^Y on the last step because Y means X, it's notational, I didn't care to do it properly by denoting the original values as X_0 and Y_0 (primarily because subscripts are a pain in the ass to do in vBulletin without a math addon).
I know what XOR is, I'm asking why he's trying to use it...