+ 1

How to Reverse a String?

Write a program to take a string as input and output its reverse. The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements. Scanner scanner = new Scanner (System. in); 7 String text scanner. nextLine (); 8 char[] arr = text.toCharArray(); Please help with this code guys I am new in programming!

6th Jan 2023, 7:54 AM
Rafiullah Ahmadi
Rafiullah Ahmadi - avatar
14 Respuestas
+ 4
Thank you for your help guys. I finally did it 😇 Here is the code: for(int i = arr.length-1; i >= 0; i- -){ System.out.print(arr[i]); }
6th Jan 2023, 11:03 AM
Rafiullah Ahmadi
Rafiullah Ahmadi - avatar
+ 6
Aditya Dixit Pls avoid giving finished code as answer, because it makes the OP to skip the most important part of learning. Always prefer giving hints for the OP to find the solution.
7th Jan 2023, 6:05 AM
Emerson Prado
Emerson Prado - avatar
+ 5
Arham Hashmi Pls don't: 1. Use other posts to ask unrelated questions. This is considered "hijacking". Open a new post instead. 2. Ask others write finished code for you. The point of SoloLearn is, well, to learn, and that requires trying. Pls see previous comments on that. Instead, show your attempt and explain your difficulties. In your own post, of course.
7th Jan 2023, 8:11 PM
Emerson Prado
Emerson Prado - avatar
+ 3
Now you can use a loop and print the string from last item up to first item.
6th Jan 2023, 8:11 AM
JaScript
JaScript - avatar
+ 2
Read about loops first. Then try this program. You need to output character array arr in reverse , that is from last index to first as start from I=are.length-1 to, until I>=0, by decreasing i each iteration. so need to add, 2 more lines to code. And missing =, in statement : String text = scanner.nextLine(); Try this, if not work then save code you tried, and share link here..
6th Jan 2023, 8:09 AM
Jayakrishna 🇮🇳
+ 2
Smith Welder Pls see my previous answer to Aditya Dixit . Having the OP finishing the solution helps a lot in learning.
7th Jan 2023, 4:23 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Well, to actually reverse a string is a thing, to output characters in a string in reverse is another. These are different, so you carefully note the task, which one it is ...
6th Jan 2023, 10:22 AM
Ipang
+ 1
Well. Just display is enough. Or you can reverse and store back, then display. but no need here... The code in description is already given in the task. Next you just need to add loop to display output of each character of arr array. How you tried that..? Try it. If not work them post your try..
6th Jan 2023, 10:44 AM
Jayakrishna 🇮🇳
+ 1
Do string chapter and loops first and then do this
7th Jan 2023, 3:49 PM
Fawaz Ahmed Khan
Fawaz Ahmed Khan - avatar
0
To be clear like this: hello world dlrow olleh
6th Jan 2023, 10:26 AM
Rafiullah Ahmadi
Rafiullah Ahmadi - avatar
0
Here is the complete code with the reverse string function added: Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); String reversed = ""; for (int i = arr.length - 1; i >= 0; i--) { reversed += arr[i]; } System.out.println(reversed);
6th Jan 2023, 6:34 PM
Aditya Dixit
0
You can use string builder it have method reverse, example: class Program { public static void main(String[] args){ System.out.println(new StringBuilder() .append("Hello World").reverse()); } } Just one line and working fast..
7th Jan 2023, 2:07 PM
Smith Welder
Smith Welder - avatar
0
So, there are two ways to reverse a String - first, using StringBuilder class and its method reverse(), and second - convert your String into CharArray and reverse it in for-loop just like any other Array.
7th Jan 2023, 4:52 PM
Viktor Malik
Viktor Malik - avatar
0
5 Can anyone give me the program of Library management system by using data structures In cpp language
7th Jan 2023, 7:25 PM
Arham Hashmi
Arham Hashmi - avatar