From ae5a90d75b43cc399395c3f17fd37830fa3ae8ac Mon Sep 17 00:00:00 2001 From: Mihindu Ranasinghe Date: Tue, 20 Oct 2020 14:44:47 +0530 Subject: [PATCH] Reversing a user given string and print it --- Interview Questions/reversing-a-string.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Interview Questions/reversing-a-string.java diff --git a/Interview Questions/reversing-a-string.java b/Interview Questions/reversing-a-string.java new file mode 100644 index 0000000..2105138 --- /dev/null +++ b/Interview Questions/reversing-a-string.java @@ -0,0 +1,21 @@ +import java.util.*; +class ReverseString +{ + public static void main(String args[]) + { + String originalString, reverseString = ""; + Scanner input = new Scanner(System.in); + + System.out.print("Enter a string to reverse : "); + originalString = input.nextLine(); + + int length = originalString.length(); + + for (int i = length - 1 ; i >= 0 ; i--){ + reverseString = reverseString + originalString.charAt(i); + + } + + System.out.println("Reverse of the string: " + reverseString); + } +} \ No newline at end of file