+ 8

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]); } } }

4th Nov 2021, 8:46 PM
Natanael
8 Answers
+ 4
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();
4th Nov 2021, 9:27 PM
Natanael
+ 3
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)); } } }
4th Nov 2021, 10:36 PM
Natanael
+ 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..
4th Nov 2021, 8:59 PM
Jayakrishna 🇮🇳
+ 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
5th Nov 2021, 6:02 PM
Natanael
0
You're welcome..
4th Nov 2021, 9:38 PM
Jayakrishna 🇮🇳
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)); } } }
5th Nov 2021, 5:47 PM
Natanael
0
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 i = arr.length - 1; i >= 0; i--){ System.out.print(arr[i]); } } }
1st Sep 2022, 5:17 AM
Fernando CM
Fernando CM - avatar
- 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.
5th Nov 2021, 6:42 AM
Billy Beagle