From 37f29504a3f5830ea04e6eb124ab50d04deac8b7 Mon Sep 17 00:00:00 2001 From: Shyam-12 <72685424+Shyam-12@users.noreply.github.com> Date: Wed, 20 Oct 2021 20:26:54 +0530 Subject: [PATCH] chore(Java): add reverse string (#591) --- algorithms/Java/README.md | 1 + algorithms/Java/strings/reverse-string.java | 36 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 algorithms/Java/strings/reverse-string.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index b891f7c6..ac44bdc0 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -72,6 +72,7 @@ - [Anagram](strings/anagram.java) - [Longest Common Substring](strings/Longest_common_substring.java) - [Boyer Moore Search](strings/Boyer_Moore.java) +- [Reverse String](strings/reverse-string.java) - [First Non Repeating Character](strings/first-non-repeating-char.java) ## Trees diff --git a/algorithms/Java/strings/reverse-string.java b/algorithms/Java/strings/reverse-string.java new file mode 100644 index 00000000..9d8c2d8b --- /dev/null +++ b/algorithms/Java/strings/reverse-string.java @@ -0,0 +1,36 @@ +/* +Write a function that reverses a string. The input string is given as an array of characters s. +sample input: +Hello + +sample output: +olleH + +Time Complexity: O(n) +*/ + +package stringProblems; + +import java.util.*; + +public class ReverseString { + public static String revString(String str) { + String reversedString = ""; + + for (int i = 0; i < str.length(); i++) { + reversedString = str.charAt(i) + reversedString; + } + return reversedString; +} + + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + + String str = s.nextLine(); + + String x = revString(str); + System.out.print(x); + + } + +}