+ 2
Reserve a string
How to solving priblem
7 odpowiedzi
+ 1
https://www.javatpoint.com/how-to-reverse-string-in-java
Search the internet before asking
+ 1
Martin Taylor most probably it's reverse a string, I guess
+ 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.
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);
}
}