1function reverseString(s){
2 return s.split("").reverse().join("");
3}
4reverseString("Hello");//"olleH"
1"this is a test string".split("").reverse().join("");
2//"gnirts tset a si siht"
3
4// Or
5const reverse = str => [...str].reverse().join('');
6
7// Or
8const reverse = str => str.split('').reduce((rev, char)=> `${char}${rev}`, '');
9
10// Or
11const reverse = str => (str === '') ? '' : `${reverse(str.substr(1))}${str.charAt(0)}`;
12
13// Example
14reverse('hello world'); // 'dlrow olleh'
1public class ReverseString {
2 public static void main(String[] args) {
3 String s1 = "neelendra";
4 for(int i=s1.length()-1;i>=0;i--)
5 {
6 System.out.print(s1.charAt(i));
7 }
8 }
9}
1def reverse_string(str):
2 return str[::-1]
3print(reverse_string("This string is reversed!"))
1
2#include <iostream>
3#include <algorithm>
4using namespace std;
5int main()
6{
7 string str = "Lap trinh khong kho";
8
9 // Reverse str[beign..end]
10 reverse(str.begin(), str.end());
11
12 cout << str;
13 return 0;
14}
15
16