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

-2

Reverse a string

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

11/30/2020 7:05:58 PM

Daniel

20 Answers

New Answer

+10

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.

+4

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--

+4

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

+4

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.

+3

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

+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

+2

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

+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.

0

True, but honestly, ive looked up how to import and use different classes before and couldn't really understand much of how it was done at all. Coding is very confusing at first

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/

0

Dustin James Locey, Exactly my point. Well agreed.

0

Lam Wei Li, Thank you for the explanation. I access SoloLearn using a browser. I was not aware of the lesson you mentioned. I do wish people would add "without using the standard libraries" to these sort of questions.

-1

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

-1

Okay this makes more sense to me. Thanks Lam!

-1

That is true, there was no need to. I honestly don't know how to find auto imported classes

-1

So I don't even know what methods they have that I can use lol

-1

Reversing a string is not difficult, why are you making it difficult Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(sc.nextLine()); System.out.println(sb.reverse());

-2

So can you explain what is happening here? Kind of confused on how it works

-2

https://code.sololearn.com/ct59Fgj3GVzW/?ref=app Hi guys😁 Please help me🙃 What is the problem? I don't know why the compiler dosn't show the result