Reversing a string: Java vs Python – minimal code, maximum impact! This is exactly why Python caught my attention over Java: less code, more performance. Sometimes, simplicity wins. 🚀 #Java #Python #coding #programming
System.out.print(new StringBuilder(str). reverse().toString()) Same simple as python 😉
Python Part would raise a SyntaxError, because you can‘t use type ‚str‘ as variable name 🤣
How do you measure performance here?
Rust is usually considered intimidating, so here's a bit of code to disprove it: // iterate over characters, reverse the order, collect into a new string let reversed = str.chars().rev().collect::<String>(); or, if it's an ASCII string: assert!(str.is_ascii()); // get raw bytes, reverse them, convert back let mut bytes = str.into_bytes(); bytes.reverse(); let reversed = String::from_utf8(bytes).expect("should be a valid ASCII string"); second approach reverses the string in-place, with no (re)allocations at all.