I started learning Javascript recently, and as a little challenge I tried to create a program where a prompt asks for a name, and then the program prints the name out in reverse. My thinking was as follows:
var name = prompt("What is your name?");name = new Array(name.length);name.reverse();document.write(name);
What's wrong with this code?
Can anyone help me with the assignment - I have to reverse a string by using def. I am not allowed to use approaches like [::-1] or .reversed...The following function works, but prints vertically:
def ex1(name): for x in range(len(name)-1,-1,-1): print(name[x])kroYweN
how do I put the letters back into horizontal order?? Anyone? Thanks!
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
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.
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.
Casa - Mapa do Site - Privacidade - Links - Copyright © 2019 Cortex IT Ltd : Contato : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see Privacidade for details). You will only see this message once.