0

26 code project (reverse a string) help

Can someone help me with this project? Here's the code that I have so far, but it doesn't seem to be working: import java.util.Scanner; import java.util.Arrays; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); String r = reverse("hello there"); System.out.println(r); } public static String reverse(String s) { char[] letters = new char[s.length()]; int letterIndex = 0; for (int i = s.length() - 1; i >= 0; i--) { letters[letterIndex] = s.charAt(i); letterIndex++; } String reverse = ""; for (int i = 0; i < s.length(); i++) { reverse = reverse + letters[i]; } return reverse; } }

7th Mar 2022, 6:46 PM
Taylor Frye
4 Answers
+ 4
Taylor Frye Why did you paas Hard Code value in reverse method? Do this: String r = reverse(text);
7th Mar 2022, 6:53 PM
A͢J
A͢J - avatar
+ 1
AJ it worked. Thank you very much.
7th Mar 2022, 7:01 PM
Taylor Frye
0
AJ ok I will try that
7th Mar 2022, 7:00 PM
Taylor Frye
0
You should also try this String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int x = arr.length - 1; x >= 0; x--){ System.out.print(arr[x]);
8th Mar 2022, 1:39 PM
Мурат Аккизов