How do you reverse a string in place (or in-place) in JavaScript when passed to a function with a return statement? All without using the built-in functions? .reverse()
, .charAt()
, etc.
Converting String into Bytes: getBytes() method is used to convert the input string into bytes[].Method:
Source
// Java program to ReverseString using ByteArray. import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "Geeks"; // getBytes() method to convert string // into bytes[]. byte [] strAsByteArray = input.getBytes(); byte [] result = new byte [strAsByteArray.length]; // Store result in reverse order into the // result byte[] for (int i = 0; i<strAsByteArray.length; i++) result[i] = strAsByteArray[strAsByteArray.length-i-1]; System.out.println(new String(result)); } }
I expect the output to be : skeg
Say you have this string:
ABCDEFGH
And you want to reverse it so that it becomes:
GHEFCDAB
What would be the most efficient / pythonic solution? I've tried a few different things but they all look horrible...
Thanks in advance!
Update:
In case anyone's interested, this wasn't for homework. I had a script that was processing data from a network capture and returning it as a string of hex bytes. The problem was the data was still in network order. Due to the way the app was written, I didn't want to go back through and try to use say socket.htons, I just wanted to reverse the string.
Unfortunately my attempts seemed so hideous, I knew there must be a better way (a more pythonic solution) - hence my question here.
I have List<String> list = Arrays.asList("A", "B", "C");
I want to join all string inside list with delimiter ,
in reverse order :
//resultString joinedString = "C,B,A";
what is the best approach to achieve this?
currently I use index loop :
String joinedString = "";List<String> list = Arrays.asList("A", "B", "C");for (int i = list.size() - 1; i >= 0; i--) { String string = list.get(i); joinedString = joinedString + string + ",";}//to remove ',' from the last stringif(joinedString.length() > 0) { joinedString = joinedString.substring(0, joinedString.length() - 1);}//Output C,B,A
So I have this function:
function toAlpha($data){ $alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $alpha_flip = array_flip($alphabet); if($data <= 25){ return $alphabet[$data]; } elseif($data > 25){ $dividend = ($data + 1); $alpha = ''; $modulo; while ($dividend > 0){ $modulo = ($dividend - 1) % 26; $alpha = $alphabet[$modulo] . $alpha; $dividend = floor((($dividend - $modulo) / 26)); } return $alpha; }}
which given a number converts it into character and it works fine
but then I also want a reverse function of this that given any output of this function, return the exact input that was put in to produce that output and I tried this:
function toNum($data){$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $alpha_flip = array_flip($alphabet); if(strlen($data) == 1){ return (isset($alpha_flip[$data]) ? $alpha_flip[$data] : FALSE); } else if(strlen($data) > 1){ $num = 1; for($i = 0; $i < strlen($data); $i++){ if(($i + 1) < strlen($data)){ $num *= (26 * ($alpha_flip[$data[$i]] + 1)); } else{ $num += ($alpha_flip[$data[$i]] + 1); } } return ($num + 25); }}
but it's not working properly...toAlpha(728) is producing 'aba' but toNum('aba') is producing 1378 rather than 728...
what did I do wrong? how can I fix the reverse function so that it works properly?
thanks in advance!
Please note that by viewing our site you agree to our use of cookies (see פרטיות for details). You will only see this message once.