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); + + } + +}