Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Java/ReverseString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.Scanner;

public class ReverseString {

/*
* This method takes a string as input
* and returns the reversed version of it.
* Example:
* Input : "hello"
* Output : "olleh"
*/
public static String reverse(String input) {
if (input == null) {
return null;
}

// Using StringBuilder for efficient reversal
return new StringBuilder(input).reverse().toString();
}

public static void main(String[] args) {

// Scanner is used to take user input from the console
Scanner sc = new Scanner(System.in);

System.out.print("Enter a string to reverse: ");
String userInput = sc.nextLine(); // Taking complete line as input

String reversed = reverse(userInput); // Calling the reverse method

System.out.println("Reversed String: " + reversed);

sc.close(); // Closing the scanner (good practice)
}
}