Reversing a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reversing a string

Need assistance with the code for reversing a string. I need to write a program to take a string as input and output its reverse. The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements. Please Help!

22nd Aug 2022, 12:04 PM
Onica Victory
8 Answers
+ 6
Onica Victory , i can not find any hint what programming language you are going to use. can you please update your post with this information? please use the tags to do so.
22nd Aug 2022, 2:21 PM
Lothar
Lothar - avatar
+ 6
Mohammad Hakim Monib , please do not give ready made codes, since the asker has not shown any attempt that he has done by himself. it does not really help the asker to develop his coding skills and building up the required experience. it is better to give hints and tips, so that the asker has the opportunity to solve the task.
26th Aug 2022, 8:12 PM
Lothar
Lothar - avatar
+ 5
gonel Could you please refrain from spoonfeeding people solutions? Sure you meant to help but that's not how people learn. Plus no one should ask others to invest time for things that they choose not to, themselves. (The OP has shown no attempt)
22nd Aug 2022, 12:58 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
string = input() a=[] r_string = string[::-1] for i in r_string: a.append(i) #first we are taking input, then we are using for loop to append letters to char array as I wrote above.
22nd Aug 2022, 12:11 PM
gonel
gonel - avatar
+ 2
gonel Thank you, have a good one! ☺️
22nd Aug 2022, 1:39 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Korkunç el Gato ok next time I won't do it like that. Thank you for advice.
22nd Aug 2022, 1:38 PM
gonel
gonel - avatar
0
Ok good idea; But sometime code sharing is good.
31st Aug 2022, 4:14 PM
Mohammad Hakim Monib
Mohammad Hakim Monib - avatar
- 3
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); String n = "45799"; String rev = ""; for(int i = n.length()-1; i>=0;i--){ rev = rev + n.charAt(i); } System.out.println(rev); } }
26th Aug 2022, 3:08 AM
Mohammad Hakim Monib
Mohammad Hakim Monib - avatar