I lost a contest today, it was about binary XOR. I had to do it in ten minutes.
2 binary numbers n1 and n2, separated by spaces.
The result of an OR between n1 and n2.
n1 and n2 have the same number of digits.
I first tried to access the length of these Strings to make a to by two comparison but I wasn't able to do it...
Furthermore tis code was too long compared to one having said having done it in 80 characters...
Can you help me learning
Thanks in advance.
Concatenate (i.e. paste together) one String on to the end of another you can use '+', as in
s3 = s3 + "abc"
This will not work. Strings are immutable, meaning that once created you can't change them. You'll have to use concatenation instead. A proper program would use a StringBuilder but I'm guessing you're not ready for that yet.
This will not work. You are using a variable 'i' when you have not yet declared it. "i=" is not a complete expression. "i--" is not a boolean expression. "i=0" will reset 'i' so it negates anything else you've done with 'i'.
This will not work. "n1.charAt(i)=0" is not a boolean expression. I'm guessing you meant to use "==". You shouldn't be comparing a character to an integer, you probably want '0' (with the single quotes).
if (n1.charAt(i)=0 && (n2.charAt(i)=0)
Antoine Compagnie wrote:Can you help me learning
How to access strings size
How to do a binary XOR
Everything what you need to know from your two listed points, is the truth table of one or another operator (better to know both). To solve this exercise, you don't need computer. You need to come up with logical program's approach.
Forget computer. You don't need to know how to access String size, it doesn't have size, it has length, anyway, what would you do with it? Try to solve it on a piece of paper. How you'd solve it by describing steps in english or other preferred language?
This is what you'd get in both cases.