Java Exercises: Reverse a string
Java Basic: Exercise-37 with Solution
Write a Java program to reverse a string.
Test Data:
Input a string: The quick brown fox
Pictorial Presentation: Reverse a string

Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise37 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input a string: ");
char[] letters = scanner.nextLine().toCharArray();
System.out.print("Reverse string: ");
for (int i = letters.length - 1; i >= 0; i--) {
System.out.print(letters[i]);
}
System.out.print("\n");
}
}
Sample Output:
Input a string: The quick brown fox Reverse string: xof nworb kciuq ehT
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to compute the distance between two points on the surface of earth.
Next: Write a Java program to count the letters, spaces, numbers and other characters of an input string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
Java: How to round a number to n decimal places in Java
Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.
DecimalFormat df = new DecimalFormat("#.####"); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); }
Output:
12 123.1235 0.23 0.1 2341234.2125
For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:
Double d = n.doubleValue() + 1e-6;
To round down, subtract the accuracy.
Ref: https://bit.ly/3a4mvz0
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises