forked from bytao7mao/java_simple_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString.java
More file actions
26 lines (19 loc) · 891 Bytes
/
reverseString.java
File metadata and controls
26 lines (19 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
static String reverse(String string){
//method 1
char[] stringAsByteArr = string.toCharArray(); //the initial string transformed into array of characters
char[] result = new char[stringAsByteArr.length]; //the new array of characters in reverse order
for (int i=0;i<stringAsByteArr.length;i++){
//for each char in reverse order, put in the new char array
result[i] = stringAsByteArr[stringAsByteArr.length-i-1];
}
return String.valueOf(result);
}
//method 2
//void reverse2(){
// char[] try1 = string.toCharArray();
// for (int i = try1.length-1; i>=0; i--)
// System.out.print(try1[i]);
// }
//method3
//easy but i feel like cheating with this method :D
// String testB = new StringBuilder("testing").reverse().toString();