Program to reverse a String.However, I receive no output and there are no build errors. What am I doing wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Program to reverse a String.However, I receive no output and there are no build errors. What am I doing wrong?

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(); //reversing a String String revText = ""; for(int i = (arr.length -1); i <= 0; i--){ revText += arr[i]; } System.out.println(revText); } }

17th May 2022, 5:50 AM
Karuchiu Wamahiga
Karuchiu Wamahiga - avatar
2 Answers
+ 2
Figured out why. The for loop test-expression should be i >=0; instead of i <= 0;
17th May 2022, 6:18 AM
Karuchiu Wamahiga
Karuchiu Wamahiga - avatar
+ 1
Try using the search bar This Question is asked frequently https://www.sololearn.com/Discuss/3016540/?ref=app
17th May 2022, 6:01 AM
Chris Coder
Chris Coder - avatar