Is it possible to reverse
String in Java without using any of the temporary variables like
Only can use
I have a very simple request, please help.
Currently I have the following string in excel "17-May-10", and I need it to be "10-May-17". So as you can see just reverse the 17 and 10. Can someone let me know how to do this by only using excel functions? Thanks in advance!
I have two solutions to reverse a string in Ruby. One prints true while the other prints false, however, both print out the response I want.
Why does one say it's false even though it results in the same answer as the solution that prints true?
Here are the solutions and the tests:
def reverse(string)new = "" i = 0 length = string.length while i < length do new = new.to_s + string[-1, 1].to_s string.chop! if i >= string.length break end end puts newenddef secondreverse(string) new = "" i = 0 length = string.length while i < length do new = string[i] + new i += 1 end return newend
These are tests to check that the code is working. After writing your solution, they should all print true.
puts("\nTests for #reverse")puts("===============================================") puts( 'secondreverse("abc") == "cba": ' + (secondreverse("abc") == "cba").to_s ) puts( 'secondreverse("a") == "a": ' + (secondreverse("a") == "a").to_s ) puts( 'secondreverse("") == "": ' + (secondreverse("") == "").to_s )puts("===============================================")
I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use the reversed method or the common answer here on stackoverflow,
Obviously in the real world of programming, one would most likely go with the extended slice method, or even using the
reversed function but perhaps there is some case where this would not work?
I present a solution below in Q&A style, in case it is helpful for people in the future.