chore(Java): add reverse string (#591)
parent
df69cbbfb3
commit
37f29504a3
|
@ -72,6 +72,7 @@
|
||||||
- [Anagram](strings/anagram.java)
|
- [Anagram](strings/anagram.java)
|
||||||
- [Longest Common Substring](strings/Longest_common_substring.java)
|
- [Longest Common Substring](strings/Longest_common_substring.java)
|
||||||
- [Boyer Moore Search](strings/Boyer_Moore.java)
|
- [Boyer Moore Search](strings/Boyer_Moore.java)
|
||||||
|
- [Reverse String](strings/reverse-string.java)
|
||||||
- [First Non Repeating Character](strings/first-non-repeating-char.java)
|
- [First Non Repeating Character](strings/first-non-repeating-char.java)
|
||||||
|
|
||||||
## Trees
|
## Trees
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue