- 16
Reverse a string
How do you reverse a string? https://code.sololearn.com/cX1t7L6iQPZ8/?ref=app
53 Respostas
+ 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.
+ 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]);
}
}
}
+ 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.
+ 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
+ 3
Эльвин Теймуров What do you mean? Maybe the curly brackets help?
String rev = "";
for (char i : arr) {
rev = i + rev;
}
System.out.println(rev);
+ 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);
}
}
+ 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
+ 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
+ 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]);
}
}
}
+ 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
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
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
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.
0
Daniel I have edited my original answer to include the explanation.
0
Okay this makes more sense to me. Thanks Lam!
0
Dustin James Locey,
Exactly my point. Well agreed.
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);
}
}
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.