strrev
strrev(string $string): string
Reverses a string.
C C23
C23
#include <stdio.h>
#include <string.h>
static void reverse(char *s) {
size_t i = 0, j = strlen(s) - 1;
while (i < j) {
char t = s[i];
s[i++] = s[j];
s[j--] = t;
}
}
int main(void) {
char buf[] = "hello";
reverse(buf);
printf("%s\n", buf);
return 0;
}
Output
olleh
C++ C++26
C++26
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s = "hello";
std::reverse(s.begin(), s.end());
std::cout << s << "\n";
return 0;
}
Output
olleh
C# v11.0.100
v11.0.100
char[] chars = "hello".ToCharArray();
Array.Reverse(chars);
Console.WriteLine(new string(chars));
Output
olleh
Go v1.27.4
v1.27.4
package main
import "fmt"
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func main() {
fmt.Println(reverse("hello"))
}
Output
olleh
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println(new StringBuilder("hello").reverse());
}
}
Output
olleh
JavaScript v26.13.1
v26.13.1
console.log("hello".split("").reverse().join(""));
Output
olleh
PHP v8.6.0
v8.6.0
echo strrev("hello");
Output
olleh
Python v3.15.2
v3.15.2
print("hello"[::-1])
Output
olleh
Rust v1.99.1
v1.99.1
fn main() {
let s: String = "hello".chars().rev().collect();
println!("{}", s);
}
Output
olleh
TypeScript v26.13.1
v26.13.1
console.log("hello".split("").reverse().join(""));
Output
olleh
Language differences
C#: Array.Reverse (strings are immutable, so convert to a char array).
C++: std::reverse.
Go: converts to []rune first and reverses multibyte characters correctly.
Java: StringBuilder.reverse.
JavaScript: split("").reverse().join(""); splits by UTF-16 code units and breaks surrogate pairs (e.g. emoji).
PHP: the built-in strrev; beware of multibyte/Unicode — strrev reverses bytes and scrambles UTF-8 sequences; this example uses ASCII hello everywhere to sidestep the differences.
Python: slice [::-1].
Rust: chars().rev().collect().
TypeScript: same as JS — split("").reverse().join("").