Difference between revisions of "Code Snippet: Print String Backwards"

From Coder Merlin
 
Line 18: Line 18:
</syntaxhighlight>
</syntaxhighlight>
[https://stackoverflow.com/questions/931092/reverse-a-string-in-python Source]
[https://stackoverflow.com/questions/931092/reverse-a-string-in-python Source]
== Java ==
<syntaxhighlight lang="java">
String string = "That Which Survives";
StringBuilder stringBuilder = new StringBuilder(string);
stringBuilder.reverse();
System.out.println(stringBuilder.toString());
// In one line
System.out.println(new StringBuilder(string).reverse().toString());
</syntaxhighlight>

Latest revision as of 18:57, 19 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Print String Backwards[edit]

Swift[edit]

let string = "That Which Survives"
for character in string.reversed() {
    print(character)
}

Python[edit]

string = "That Which Survives"
# Short & Fast
print(string[::-1])
# More readable
print(''.join(reversed(string)))

Source

Java[edit]

String string = "That Which Survives";
StringBuilder stringBuilder = new StringBuilder(string);
stringBuilder.reverse();
System.out.println(stringBuilder.toString());

// In one line
System.out.println(new StringBuilder(string).reverse().toString());