Reverse a string | Sololearn: Learn to code for FREE!

-12

Reverse a string

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

11/30/2020 7:05:58 PM

Daniel

51 Answers

New Answer

+23

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.

+10

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.

+5

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]); } } }

+4

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

+4

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); } }

+3

Don't make your life difficult by reinventing the wheel, read the docs... https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#reverse--

+3

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

+3

Эльвин Теймуров What do you mean? Maybe the curly brackets help? String rev = ""; for (char i : arr) { rev = i + rev; } System.out.println(rev);

+2

Okay this makes more sense to me. Thanks Lam!

+2

Hi. Why I have mistake? This is my code: mport java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int i = arr.length-1;i>=0;i--) { System.out.print(arr[i]); } } }

+1

https://code.sololearn.com/cK9zqLHiHtSM/?ref=app

+1

Lam Wei Li, Java Strings are immutable. That means they cannot be altered. Every iteration of your loop the line.... rev = i + rev; causes a new String object to be created and the old one destroyed, This will cause a performance hit. The StringBuilder class was specifically designed for building strings without having to destroy and recreate objects.

+1

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

+1

Lam Wei Li can you explain, please, why does for loop here work without statement? String rev = ""; for (char i : arr) rev = i + rev; System.out.println(rev);

+1

Lam Wei Li yes I meant curly brackets. Why it works without them?

+1

Эльвин Теймуров Defaults to the next line of code ONLY when no brackets.

+1

Эльвин Теймуров It is possible but at the expense of complexity of two O(n) loops and harder to read.

+1

AML Yakin The explanation is in the top voted and marked as correct answer. You need to scroll all the way up.

0

Daniel I have edited my original answer to include the explanation.

0

The SoloLearn courses only teach the basics of each language. This is true for Java, Python, C, C++, C#, and JavaScript. It is not possible to go into every detail of every function or method in the standard libraries. The basic knowledge gained from these courses enables you to understand the documentation that comes with the standard libraries. The Java API contains over 5,000 classes, and each of those classes has multiple methods. The C/C++ standard libraries has dozens of header files. The Python standard library is huge. It's up to you to read the documentation if you want to learn to program. Some useful links: https://docs.oracle.com/javase/tutorial/ https://docs.oracle.com/javase/8/docs/ https://docs.oracle.com/javase/8/docs/api/ https://docs.oracle.com/en/java/javase/11/ https://docs.oracle.com/en/java/javase/14/