+ 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!
14 Réponses
+ 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]);
}
+ 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.
+ 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.
+ 3
Now you can use a loop and print the string from last item up to first item.
+ 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..
+ 2
Smith Welder Pls see my previous answer to Aditya Dixit . Having the OP finishing the solution helps a lot in learning.
+ 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 ...
+ 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..
+ 1
Do string chapter and loops first and then do this
0
To be clear like this:
hello world
dlrow olleh
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);
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..
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.
0
5
Can anyone give me the program of Library management system by using data structures
In cpp language