+ 1
what's the problem with this 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 []arr = new char [text.length()]; int l=0; for(int i=text.length()-1;i>=0;i--) { char arr[l]=text.charAt(i); l++; } String reverse =""; for (int i=0;i<text.length();i++) { reverse+=arr[i]; } System.out.println(reverse); } }
3 Answers
+ 3
This code works correctly:
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();
String reverse ="";
for (int i=0;i<text.length();i++)
{
reverse+=arr[text.length()-1-i];
}
System.out.println(reverse);
}
}
+ 2
1. You created arr array twice.
2. reverse+=arr[text.length() -1- i]
+ 1
thank you bro