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

-14

Reverse a string

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

11/30/2020 7:05:58 PM

Daniel

57 Answers

New Answer

+27

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.

+9

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

+7

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

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

+3

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

+2

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

+2

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

+1

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

+1

Okay this makes more sense to me. Thanks Lam!

+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

Эльвин Теймуров 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.

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

0

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

0

Dustin James Locey, Exactly my point. Well agreed.

0

import java.util.Scanner; public class Dt { public static void main(String[] args) { String m = "Good morning",reverse=""; char[] ch = m.toCharArray(); for(int i=m.length()-1;i>=0;i--) { reverse += ch[i]; } System.out.print(reverse); } }

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