Reverse String Project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Reverse String Project

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[] result = new char[arr.length]; //store the result in reverse order into the result char[] for(int i=0; i<arr.length; i++) { result[i] = arr[arr.length - i - 1]; } System.out.println(new String(result)); } } Here I will check all testcases successfully

13th Jan 2021, 8:50 AM
AYYAPARAJA K
AYYAPARAJA K - avatar
9 Answers
+ 2
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.
18th Jan 2021, 4:59 AM
Lam Wei Li
Lam Wei Li - avatar
+ 2
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(); for(int i=arr.length-1;i>=0;i--) { System.out.println(arr[i]); }// Just we start printing from back side } }
14th Jun 2021, 4:27 PM
Dharmi Sri
Dharmi Sri - avatar
+ 1
OK. Thank you
13th Jan 2021, 9:06 AM
AYYAPARAJA K
AYYAPARAJA K - avatar
0
K. Ayyaparaja This is your problem?
13th Jan 2021, 8:54 AM
A͢J
A͢J - avatar
0
Yes
13th Jan 2021, 8:56 AM
AYYAPARAJA K
AYYAPARAJA K - avatar
0
K. Ayyaparaja Your program is looking correct. I don't think there would be any issue to pass all test cases.
13th Jan 2021, 9:02 AM
A͢J
A͢J - avatar
0
Martin Taylor 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.
18th Jan 2021, 5:01 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(); char [] result = new char[arr.length]; for(int i = arr.length-1;i >=0;i--) { System.out.println(arr[i]);} This is the code that I'm using, however it's printing out in an array instead of as a string, how do I get this to change?
5th Sep 2022, 12:59 PM
Onica Victory
- 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(); for(i=0;i<arr.length;i--){System.out.println(arr[i])}; //your code goes here }} this is my code is it correct?
26th Dec 2022, 7:39 PM
Jasaret
Jasaret - avatar