Reverse a String | Sololearn: Learn to code for FREE!

+3

Reverse a String

Hello, I completed the Challenge Rerverse a String in Java, but Im not sure if this is the way of clean coding. It felts wrong :D If i try to do the decrement into the head of the Loop i will get an index out of bounds Exception. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int i = arr.length; i>0;){ System.out.print(arr[--i]); } } }

7/25/2021 11:26:23 AM

Pascal

10 Answers

New Answer

+5

Not gonna comment on the "clean coding" theory. Just about one thing, this code outputs each character of the String in reverse order, not creating a new String which contains the reversed content of the original String. If the task asked to output characters of a String in reverse order, this is correct, I guess.

+4

Pascal Yes you will get exception because array index start from 0 so if there is string "abc" and you do arr[3] then you will get exception because of index 0 1 2 but you are accessing 3rd. So to avoid this problem you can do this: for (int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i]); }

+4

If you need to preserve the reversed string this works nicely and utilizes the loop efficiently... char [] array = origString.toCharArray(); for(int i=0; i<array.length/2; i++) { char character = array[i]; array[i] = array[array.length -i -1]; array[array.length -i -1] = character; } String reversed = new String(array);

+3

Thanks for the feedback guys, right it was suppose to use an array. The give me a string an converted it to an char array. I should reverse that array in an Loop and put it out. System.out.println(new StringBuilder(string).reverse().toString()); I know i could reverse a string with an Stringbuilder but i was suppose to use an for loop and an array Thank you very much. greets Pascal

+3

Pascal For solutions suggesting the standard library, that itself defeat the purpose of the Challenge in the first place. And for the solution you pasted above, it isn't elegant and doesn't reverse a String, even though it iterates and prints each character from the back. For the solutions suggested creating new String within each iteration of the loop, that's also a very expensive operation and very bad. The best solution in this thread is that of Paul K Sadler but he made it less readable, also using new operator to create a String A modified and readable version of it is String reverse(String str){ char[] els = str.toCharArray(); for(int i = 0; i < els.length / 2; i++){ char temp = els[i]; els[i] = els[(els.length - 1) - i ]; els[(els.length - 1) - i] = temp; } return String.valueOf(els); } This is the most elegant and efficient solution I can think of atm You can test it here: https://code.sololearn.com/c2J1hYTdtaXG/?ref=app

+3

maleeqB Since String.valueOf() returns a new String() internally, isn't it identical to Paul K Sadler's version? (Except refactored into a method). Anyway, Kent Beck and Bob Martin (and the "clean code" gurus) would probably argue that reversal and swapping should be split into separate methods: https://code.sololearn.com/ca1A24a24a22 ... for elegance, purity, and "readability".

+1

maleeqB Since String.valueOf() returns a new String() internally, isn't it identical to Paul K Sadler's version? (Except refactored into a method). Anyway, Kent Beck and Bob Martin (and the "clean code" gurus) would probably argue that reversal and swapping should be split into separate methods: https://code.sololearn.com/ca1A24a24a22 ... for elegance, purity, and "readability". Why are you dividing length by 2? swap(arr, i, arr.length - i - 1); And this part, why arr.length - i - 1

+1

Pascal The length is divided by two for loop termination, because by working from the two ends swapping characters inwatds you only need 1/2 the length loop iterations to swap characters and meet in the middle.

0

Easiest way: You can use StringBuffer/StringBuilder. Smarter way: String reverse = ""; for (int i = text.length() - 1; i >= 0; i--) reverse += text.charAt(i); //there you go :)