Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C
- Clojure
- Clojure
- Cobol
- C++
- C#
- C#
- D
- Dart
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
(let [s "hello"
t (apply str (reverse s))]
t)
Strings are treated as sequential collections of characters. reverse returns the character list in reverse order, and apply takes this collection and feeds it as arguments into str to return a full reversed string.
As Clojure data structures are immutable, we can guarantee that s is unaltered.
As Clojure data structures are immutable, we can guarantee that s is unaltered.
IDENTIFICATION DIVISION.
PROGRAM-ID. reverse string.
PROCEDURE DIVISION.
MOVE FUNCTION REVERSE(s) TO t
STOP RUN.
string t = string.Create(s.Length, s, static (span, s) =>
{
s.AsSpan().CopyTo(span);
span.Reverse();
});
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
string t = new string(charArray);
var t = s.split('').reversed.join();
var t = new String.fromCharCodes(s.runes.toList().reversed);
This does reverse the order of the runes, however it may handle improperly some dependant runes like diacritics.
t = String.reverse(s)
T = lists:reverse(S)
character(len=:), allocatable :: t
integer :: i, n
allocate (t, mold=s)
n = len(s)
do i = 0, n - 1
t(n-i : n-i) = s(i+1 : i+1)
end do
func reverse(s string) string {
if len(s) <= 1 {
return s
}
var b strings.Builder
b.Grow(len(s))
for len(s) > 0 {
r, l := utf8.DecodeLastRuneInString(s)
s = s[:len(s)-l]
b.WriteRune(r)
}
return b.String()
}
This version of reverse takes care of multi-byte runes, but performs a single allocation.
t = reverse s :: String
var t = s.split("").reverse().join("");
String t = new StringBuilder(s).reverse().toString();
StringBuilder is available since Java 1.5
char a[] = s.toCharArray(), c;
int i, m = a.length, n = m-- / 2, z;
for (i = 0; i < n; ++i) {
c = a[i];
a[i] = a[z = m - i];
a[z] = c;
}
String t = new String(a);
String t = s.chars()
.mapToObj(x -> valueOf((char) x))
.reduce((a, b) -> b + a)
.get();
String t = "";
for (char c : s.toCharArray())
t = c + t;
(reverse s)
function utf8.reverse(s)
local r = ""
for p,c in utf8.codes(s) do
r = utf8.char(c)..r
end
return r
end
t = utf8.reverse(s)
if string s is an ascii string you can directly use string.reverse instead.
You will also need Lua5.3 or an utf8 module
You will also need Lua5.3 or an utf8 module
for ($i=0;$i<mb_strlen($s);$i++) {
$characters[] = mb_substr($s, $i, 1, 'UTF-8');
}
$characters = array_reverse($characters);
$t = implode($characters);
This solution needs mb (multibyte) extension.
function reverse(const str: string): string;
var
i, j: Integer;
begin
j := length(str);
setlength(reverse, j);
for i := 1 to j do
reverse[i] := str[j - i + 1];
end;
Function ReverseString(const AText: string): string;
var
i,j:longint;
begin
setlength(result,length(atext));
i:=1; j:=length(atext);
while (i<=j) do
begin
result[i]:=atext[j-i+1];
inc(i);
end;
end;
my $s = 'cafe' . "\N{COMBINING ACUTE ACCENT}";
my $t = join '', reverse $s =~ /\X/g;
Bet your programming language is not Unicode compliant. Perl, in general, is. Sad state of affairs: https://www.azabani.com/pages/gbu/
As of 2019-09-29, all other solutions are wrong AFAICT with respect to Unicode marks. (Dart has a disclaimer.)
Correct result: "e\N{U+0301}fac"
Wrong result: "\N{U+0301}efac"
As of 2019-09-29, all other solutions are wrong AFAICT with respect to Unicode marks. (Dart has a disclaimer.)
Correct result: "e\N{U+0301}fac"
Wrong result: "\N{U+0301}efac"
t = s[::-1]
t = ''.join(reversed(s))
s.reverse
(define t (list->string (reverse (string->list s))))
s is unaltered
t := s reversed.
Dim string_t = StrReverse(string_s)