+ 5

How can someone reverse String without recursion?

19th May 2019, 7:46 PM
Athman Hassan
Athman Hassan - avatar
4 ответов
+ 9
You can do this: String str = "MyString"; String reverse = ""; for(int i = str.length() - 1; i >= 0; i--){ //print: System.out.print(str.charAt(i); //or if you need a new String: reverse += "" + str.charAt(i); }
19th May 2019, 10:53 PM
Denise Roßberg
Denise Roßberg - avatar
+ 6
In python, you can reverse a string with just one line of code: str = "reverse" str = str[::-1] print(str) /*output is "esrever"*/
19th May 2019, 7:52 PM
Airree
Airree - avatar
+ 2
you can use for loop or while loop to do that
19th May 2019, 8:02 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Easiest way to print reverse string :) name = "nraeLoloS" print(name[::-1])
19th May 2019, 10:17 PM
Muhammad Tahir Iqbal
Muhammad Tahir Iqbal - avatar