Reserve a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Reserve a string

How to solving priblem

7th Oct 2021, 12:30 AM
Abdulmunaf Ali
Abdulmunaf Ali - avatar
4 Answers
7th Oct 2021, 2:20 AM
Rishi
Rishi - avatar
+ 1
Martin Taylor most probably it's reverse a string, I guess
7th Oct 2021, 1:05 PM
Rishi
Rishi - avatar
+ 1
Martin Taylor thanks for clarifying and taking the time to present an alternative way to reverse a string. I like your solution better than my presented solution. You're right your way of reversing a string is more efficient and also requires less code. When it comes to just reversing a single string however I don't think that the end user mentions any performance issues due to the garbage collector working overtime.
8th Oct 2021, 9:02 AM
Angelo Plevnalis
Angelo Plevnalis - avatar
0
You could use a for loop and the charAt() String method like this: public class Program { public static void main(String[] args) { // String to be reversed String text = "hello world"; // empty string to store the reversed string String reverseText = ""; //reversing the string with a for loop for(int i = text.length() - 1; i >= 0; i--) { reverseText += text.charAt(i); } // output the reversed string in the console System.out.println(reverseText); } }
7th Oct 2021, 8:10 PM
Angelo Plevnalis
Angelo Plevnalis - avatar