chore(Java): add reverse string (#591)

pull/609/head
Shyam-12 2021-10-20 20:26:54 +05:30 committed by GitHub
parent df69cbbfb3
commit 37f29504a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

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