+ 1
How to solve reverse String lesson in java...?
8 Antworten
- 3
I could, but then what would you learn? You should try to code it yourself. If you have trouble after trying and reviewing the course, then save your code to the playground and post a link to your code and explain the issue(s) that you're having, what you've tried etc. Then we will help you with your issues.
+ 6
Loop over the string from back to front outputting each char with the print method instead of println.
+ 4
String rev = "";
for (char i : arr)
rev = i + rev;
System.out.println(rev);
+ 3
Ok i am trying
+ 1
here is an examle of 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();
for(int i=arr.length-1;i>=0;i--){
System.out.print(arr[i]);
}
//your code goes here
}
}
0
Can u write it ...?😊
0
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.