Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
if the input is abcd , the answer is abcd why ?
and what does "" do is it null ?

public static String mystery(String input) {
String tricky = ""; 
for (int i = 0; i < input.length(); i++) {
tricky = input.charAt(input.length() − i − 1) + tricky;} 
return tricky;}


What I have tried:

-------------------------------------------------
Posted
Updated 2-Apr-18 18:11pm
v3

Because it is what is requested by code.
What you don't understand in your code ? is it your code ? what result did you expected ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables and see it perform.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
When you're learning, you need to have a pen and paper handy in order to understand the concepts. It takes a little practice to do this is your head and even after that people need to write things down in order to keep track of things when the problem is more complex in nature.

Here you ask two questions. I will answer the second question first
Quote:
what does "" do is it null ?


"" does not mean null.
string tricky = "";
Just means that an empty string is assigned to the variable tricky.
Hence tricky.length() will result in zero. Whereas,
string tricky;
means that the variable tricky has not been assigned a value yet (in other words tricky is null). Hence tricky.length() will result in a null pointer exception because you cannot call methods on null variables.
This answers your second question.

Now for your first question.
The for loop that is written in the method "mystery" is directed to run from when "i" is 0 till "i" reaches a value which is one less than the length of the "input", incrementing the value of "i" by one each time.
For a better understanding of loops in Java you can head over here. Now that is established we can go ahead and try to figure out what happens when you call the method with input "abcd".
Here input.length() is 4, hence the loop will execute till the test condition i < input.length() fails i.e. i reaches the value 4.

For the first iteration value of i is 0, for which the test condition is true,
so the statement inside the loop executes.
Hence input.charAt(4-0-1) results to input.charAt(3), and since the indexing of any string starts with 0, index of "a" = 0, "b" = 1, c = "2" and d = "3" in the string "abcd" so the charAt(3) will result in "d".
The next statement concatenates this to tricky which is "" resulting in "d", next it assigns the result to tricky changing its value from "" to "d", and lastly increments the value of "i" making it 1.

The loop again goes to the test condition which comes out to be true and the process is repeated, only this time the values if "i" and "tricky" are 1 and "d" respectively.
Hence, input.charAt(4-1-1) = input.chatAt(2) = "c", next concatenation with tricky
"c" + "d" = "cd" and assign this value to tricky. Lastly, increment the value of "i" by 1 making it 2.

Test the condition again 2 < 4 = true, loop executes again
i = 2, tricky = "cd"
input.charAt(4-2-1) = input.charAt(1) = "b", concatenate to tricky "b" + "cd" = "bcd", assign this value to tricky, and increment "i" by 1 making it 3.

Test the condition again 3 < 4 = true, loop executes again
i = 3, tricky = "bcd"
input.charAt(4-3-1) = input.charAt(0) = "a", concatenate to tricky "a" + "bcd" = "abcd", assign this value to tricky, and increment "i" by 1 making it 4.

Test the condition again 4 < 4 = false, loop terminates.

The next statement in line for execution is,
return tricky;
which returns the value of tricky which is "abcd".
There you go, whew.
 
Share this answer
 
v2
Comments
Member 13676546 5-Apr-18 9:56am    
thanks a lot for the explanation It was very clear useful, It seems I had problem with concatenation .. i thought it the answer would be reverse of abcd.. thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900