- 16

Reverse a string

How do you reverse a string? https://code.sololearn.com/cX1t7L6iQPZ8/?ref=app

30th Nov 2020, 7:05 PM
Daniel
60 odpowiedzi
+ 33
Using for-each for simplicity. 4 lines of code added. https://code.sololearn.com/cig6PA96rplT/?ref=app Your original code has splitted the String into an Char[] array. For-each loop loops through the Char[] array, one Char (i) at a time, from left-to-right order. String rev is the temporary result. "rev = i + rev" means you are adding the i to the front, reversing the String.
30th Nov 2020, 7:12 PM
Lam Wei Li
Lam Wei Li - avatar
+ 21
My Solution is very easy: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here int l = arr.length; //l-1 because Java starts counting at 0 for(int i = l-1; i>=0; i--){ System.out.print(arr[i]); } } }
21st Dec 2020, 10:14 PM
Pasco
+ 9
Martin Taylor, I understand your perspective. The post is a coding question in SoloLearn, under Java, Arrays, after Module 3 Quiz (accessible only on mobile). The existing codes have already split into char[] to demonstrate the understanding of Arrays. One, in any queries, we should not be modifying the pre-existing codes. Two, for this case, we should not code using StringBuilder as that defeats the overall purpose of the Arrays lesson. Just because a library is there, doesn't mean we should use it to forgo learning objectives. Likewise, if the lesson asks to code a function to calculate powers of value, we shouldn't be using Math.pow() but instead code a loop. You can disagree with me, but this is my point. I would agree with you, if the context is regarding optimisation/performance/general use in public domain.
2nd Dec 2020, 9:48 AM
Lam Wei Li
Lam Wei Li - avatar
+ 8
They are probably new to coding. I have no idea how to use StringBuilder. This app doesn't teach you that at all. Maybe thats why they downvoted it. Maybe they didn't understand it
1st Dec 2020, 5:08 AM
Dustin James Locey
Dustin James Locey - avatar
+ 3
Эльвин Теймуров What do you mean? Maybe the curly brackets help? String rev = ""; for (char i : arr) { rev = i + rev; } System.out.println(rev);
29th Apr 2021, 4:51 PM
Lam Wei Li
Lam Wei Li - avatar
+ 3
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here char[] arr2 = new char[arr.length]; for(int i = 0;i<arr.length;i++) { arr2[(arr.length-1) - i] = arr[i]; } System.out.println(arr2); } }
1st Feb 2022, 6:09 PM
Avrian Shandy
Avrian Shandy - avatar
+ 2
Here's my code, ways to reverse a string. It's a messy code just for fun,some of them are not practical but you can find lot of ways to do this task. https://code.sololearn.com/cAwau3XYE66N/?ref=app
2nd Dec 2020, 3:36 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
People doing that task, have no idea what string builder is or even how to implement it. The point of the task is to use what you HAVE learned so far. Which is how I did mine. So its 100% not wrong or bad coding the way I completed it. I completed it the way it was intended
2nd Dec 2020, 3:56 PM
Dustin James Locey
Dustin James Locey - avatar
1st Dec 2020, 3:24 AM
Dustin James Locey
Dustin James Locey - avatar
+ 1
allow me to share my code here: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here for(int i = arr.length-1; i >= 0; i--) { System.out.print(arr[i]); } } }
22nd Mar 2021, 1:10 AM
Lucky Junihardi
Lucky Junihardi - avatar
+ 1
Lam Wei Li yes I meant curly brackets. Why it works without them?
29th Apr 2021, 4:56 PM
Эльвин Теймуров
Эльвин Теймуров - avatar
+ 1
Эльвин Теймуров It is possible but at the expense of complexity of two O(n) loops and harder to read.
29th Apr 2021, 5:25 PM
Lam Wei Li
Lam Wei Li - avatar
+ 1
This is my code import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here char[] arr2 = new char[arr.length]; for(int i = 0;i<arr.length;i++) { arr2[(arr.length-1) - i] = arr[i]; } System.out.println(arr2); } }
6th Jun 2021, 2:08 AM
W.W.A. Gayan Malinda
+ 1
AML Yakin The explanation is in the top voted and marked as correct answer. You need to scroll all the way up.
6th Sep 2021, 3:22 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here for(int i=arr.length-1;i>=0;i--) System.out.print(arr[i]); } } That's the code, now to explanation: As intended in app learning program we are reversing the string with for loop. In order to do so you create a loop and make 3 statements: i=arr.length-1 - this one takes int i to interact with all of the array positions and - 1 at the end makes it to start the loop from last value in the array; i>=0 - with this statement you set the loop to go on until it reaches the last value in the array; i-- - here you just simply decrement the value of an array by 1 so it's counting down. At the end you just print the whole array for integer i. I'm not sure if I understand it correctly. If I'm wrong at some point please add the comment to make it clear.
8th Sep 2022, 5:13 AM
Hasarel
Hasarel - avatar
0
Daniel I have edited my original answer to include the explanation.
1st Dec 2020, 3:18 AM
Lam Wei Li
Lam Wei Li - avatar
0
Okay this makes more sense to me. Thanks Lam!
1st Dec 2020, 3:31 AM
Daniel
0
Dustin James Locey, Exactly my point. Well agreed.
2nd Dec 2020, 4:15 PM
Lam Wei Li
Lam Wei Li - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here String rev = ""; for (char i : arr) rev = i + rev; System.out.println(rev); } }
2nd Feb 2021, 10:21 AM
Namrata Dattani
Namrata Dattani - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here for (int i = try1.length - 1; i >= 0; i--) System.out.print(try1[i]); } } not working in the sololearn,working in the reality.
18th Feb 2021, 11:37 AM
Aleksandar Sokolovic