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

+4

Reverse a string

My original code had a loop like this for (int t = arr.length; t >= 0; t- -){ } It did not work so I got this code to work after reading a post here, it was only missing this part from the loop: int a = arr.length; for(int t = a -1; Now it works but I would like to know how to display the results in one line instead of scrolling down. Sorry for the explanation above, I was only trying to show that there was no way I would have figured this out with what I know about coding up to now. 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(); //your code goes here int a = arr.length; for (int t = a - 1; t >= 0; t--){ System.out.println(arr[t]); } } }

11/4/2021 8:46:49 PM

Natanael

10 Answers

New Answer

+2

Instead of System.out.println(), Use System.out.print(arr[I] ); println() adds line break while print() don't adds. edit: last index in array is array.length-1 because index starts from 0..

+2

Thank you so much, I was scratching my head because this seems like a simple program, so I had no idea why it was not working, the only problem was the loop initiation for( int t = arr.length -1; Something so simple. I see what's going on array elements start at zero but we normally count starting at # 1. Also thanks for showing me about the print statement, so far up to now I had only used System.out.println();

+2

I replaced the code with your suggestion but the statement char [ ] arr = text.toCharArray(); /* has to be there otherwise it won't work, I think that as long as you're declaring this statement, memory is allocated right for a char array right? Just trying to understand */ 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(); //your code goes here for (int t = arr.length - 1; t >= 0; t--){ System.out.print(text.charAt(t)); } } }

+1

Martin Taylor, sorry I did not type the for loop exactly as you mentioned, now the code works without the char[ ] line. for (int t = text.length() - 1; t >= 0; t--) As you said this code works more efficiently without using an array, it would have been better if they had shown both ways to do this

0

0

Martin Taylor, I commented out the char[ ] statement as you suggested and changed the for loop, but now I'm getting this error message "cannot find symbol" 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(); //your code goes here for (int t = text.length - 1; t >= 0; t--){ System.out.print(text.charAt(t)); } } }

-1

<int t = arr.lenght - 1> works since the first element in the array has index 0, which means that the last element would have index 16 if the arr.lenght is equal to 17 (as example), since the arr.lenght is counted starting with 1 and not 0, so each index would be subtracted. I just realised it today and I actually finished that project only a few minutes ago.