You can make use of StringBuilder#reverse() method like this:
String reverse = new StringBuilder(new String(letters)).reverse().toString();
Answer from anubhava on Stack OverflowYou can make use of StringBuilder#reverse() method like this:
String reverse = new StringBuilder(new String(letters)).reverse().toString();
I believe what you wrote is the signature of the method you have to create.
public void printReverse(char[] letters, int size){
//code here
}
You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:
for (int i = array.length-1; i >= 0 ; i--){
System.out.print(array[i]);
}
In terms of time complexity, they're both O(n). Performance wouldn't be significant here.
Which to choose? None. I would use a ready method StringBuilder#reverse:
String reversed = new StringBuilder(new String(c)).reverse().toString();
If I wanted to choose one from the two methods you posed, I would have choose the first one, it have only one loop and it's straightforward, no methods will be called, no helper objects will be created; you simply create a new array and directly push to it the new elements.
@MarounMaroun's answer will work for char arrays that are really strings. If you are worried only about those two methods then the first involves less heap allocations and GC.
However in general for an array I would use neither of your methods, but instead:
int len = c.length;
for(int i = 0; i < len / 2; i++) {
char ch = c[i];
c[i] = c[len - i - 1];
c[len - i - 1] = ch;
}
It is:
- shorter to type than method 2
- only iterates for half the array length (it is still O(n) due to the ops per iteration)
- will work for any array type
- doesn't need extra object allocations (vs Method 2) or two arrays (vs Method 1)
I would, however, be careful of micro-optimizations like this. The difference between this and Method 1 is probably minimal for small array allocations so you are better off using the one that is easiest for you to understand. (Similarly I only pulled the len variable out for clarity - some microbenchmarks claim it speeds up loops, but the downside is it pollutes your local variables with something that is only used inside the loop).
Videos
StringBuilder have a reverse method.
System.out.print("REVERSE WORD: " + new StringBuilder(word).reverse().toString());
Or if you don't want to use inbuilt methods
String result= "";
for(int i=word.length(); i>0; i--) {
result+= word.charAt(i-1);
}
System.out.print("REVERSE WORD: " + result);
public class test {
public static void main(String[] args) {
String word = "Stack Overflow";
char[] wordArray = word.toCharArray();
System.out.println("NORMAL WORD=" + Arrays.toString(wordArray));
test.reverse(wordArray);
System.out.println("REVERSE WORD=" + Arrays.toString(wordArray));
}
public static void reverse(char[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
char tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}
Output:
NORMAL WORD=[S, t, a, c, k, , O, v, e, r, f, l, o, w]
REVERSE WORD=[w, o, l, f, r, e, v, O, , k, c, a, t, S]
You are reading and writing to the same array. This:
char c2[]=c;
is not correct
You want:
char c2[]= new char[c.length];
There is no real need to use two arrays, and when you do a reference copy you end up working on the same array anyway.
Remember to test your algorithm using an empty array, null, an array with even length and one with odd length.
Here is a single array solution including simple test prints.
public class ReverseTest {
public static String reverse(String s) {
if (s == null)
return null;
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length/2; i++) {
int j = ca.length-i-1;
char c = ca[i];
ca[i] = ca[j];
ca[j] = c;
}
return String.valueOf(ca);
}
public static void main(String[] args) {
System.out.println(reverse(null));
System.out.println(reverse(""));
System.out.println(reverse("54321"));
System.out.println(reverse("4321"));
}
}
Prints:
null
empty line
12345
1234