WeixiangYan/CodeTransOcean huggingface dataset:
Task: Multi-dimensional array


C++: #include <iostream>
#include <vector>


template<typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& c) {
    auto it = c.cbegin();
    auto end = c.cend();

    out << '[';
    if (it != end) {
        out << *it;
        it = std::next(it);
    }
    while (it != end) {
        out << ", " << *it;
        it = std::next(it);
    }
    return out << ']';
}

void fourDim() {
    using namespace std;

    
    vector<vector<vector<vector<int>>>> arr;
    int cnt = 0;

    arr.push_back(vector<vector<vector<int>>>{});
    arr[0].push_back(vector<vector<int>>{});
    arr[0][0].push_back(vector<int>{});
    arr[0][0][0].push_back(cnt++);
    arr[0][0][0].push_back(cnt++);
    arr[0][0][0].push_back(cnt++);
    arr[0][0][0].push_back(cnt++);
    arr[0].push_back(vector<vector<int>>{});
    arr[0][1].push_back(vector<int>{});
    arr[0][1][0].push_back(cnt++);
    arr[0][1][0].push_back(cnt++);
    arr[0][1][0].push_back(cnt++);
    arr[0][1][0].push_back(cnt++);
    arr[0][1].push_back(vector<int>{});
    arr[0][1][1].push_back(cnt++);
    arr[0][1][1].push_back(cnt++);
    arr[0][1][1].push_back(cnt++);
    arr[0][1][1].push_back(cnt++);

    arr.push_back(vector<vector<vector<int>>>{});
    arr[1].push_back(vector<vector<int>>{});
    arr[1][0].push_back(vector<int>{});
    arr[1][0][0].push_back(cnt++);
    arr[1][0][0].push_back(cnt++);
    arr[1][0][0].push_back(cnt++);
    arr[1][0][0].push_back(cnt++);

    cout << arr << '\n';
}

int main() {
    

    fourDim();

    return 0;
}









Fortran:       DIMENSION A(5,4,3,2)                 
      X = 3*A(2,I,1,K)                     
      A(1,2,3,4) = X + 1
Task: Multi-dimensional array

C: 
int a[10];



float b[3][2];



char c[4][5][6];



double d[6][7][8][9];



int e[][3];



float f[5][4][];


int *g;


float **h;


double **i[];



char **j[][5];










Fortran:       DIMENSION A(5,4,3,2)                 
      X = 3*A(2,I,1,K)                     
      A(1,2,3,4) = X + 1
Task: Multi-dimensional array





Java: public class MultiDimensionalArray {
    public static void main(String[] args) {
        
        int m = 1;
        int[][][][] a4 = new int[5][4][3][2];
        for (int i = 0; i < a4.length; ++i) {
            for (int j = 0; j < a4[0].length; ++j) {
                for (int k = 0; k < a4[0][0].length; ++k) {
                    for (int l = 0; l < a4[0][0][0].length; ++l) {
                        a4[i][j][k][l] = m++;
                    }
                }
            }
        }

        System.out.println("First element = " + a4[0][0][0][0]);  
        a4[0][0][0][0] = 121;                                     
        System.out.println();

        for (int i = 0; i < a4.length; ++i) {
            for (int j = 0; j < a4[0].length; ++j) {
                for (int k = 0; k < a4[0][0].length; ++k) {
                    for (int l = 0; l < a4[0][0][0].length; ++l) {
                        System.out.printf("%4d", a4[i][j][k][l]);
                    }
                }
            }
        }
    }
}






Fortran:       DIMENSION A(5,4,3,2)                 
      X = 3*A(2,I,1,K)                     
      A(1,2,3,4) = X + 1
Task: Multi-dimensional array







Python: >>> from pprint import pprint as pp   
>>> from itertools import product
>>> 
>>> def dict_as_mdarray(dimensions=(2, 3), init=0.0):
...     return {indices: init for indices in product(*(range(i) for i in dimensions))}
... 
>>> 
>>> mdarray = dict_as_mdarray((2, 3, 4, 5))
>>> pp(mdarray)
{(0, 0, 0, 0): 0.0,
 (0, 0, 0, 1): 0.0,
 (0, 0, 0, 2): 0.0,
 (0, 0, 0, 3): 0.0,
 (0, 0, 0, 4): 0.0,
 (0, 0, 1, 0): 0.0,
...
 (1, 2, 3, 0): 0.0,
 (1, 2, 3, 1): 0.0,
 (1, 2, 3, 2): 0.0,
 (1, 2, 3, 3): 0.0,
 (1, 2, 3, 4): 0.0}
>>> mdarray[(0, 1, 2, 3)]
0.0
>>> mdarray[(0, 1, 2, 3)] = 6.78
>>> mdarray[(0, 1, 2, 3)]
6.78
>>> mdarray[(0, 1, 2, 3)] = 5.4321
>>> mdarray[(0, 1, 2, 3)]
5.4321
>>> pp(mdarray)
{(0, 0, 0, 0): 0.0,
 (0, 0, 0, 1): 0.0,
 (0, 0, 0, 2): 0.0,
...
 (0, 1, 2, 2): 0.0,
 (0, 1, 2, 3): 5.4321,
 (0, 1, 2, 4): 0.0,
...
 (1, 2, 3, 3): 0.0,
 (1, 2, 3, 4): 0.0}
>>>




Fortran:       DIMENSION A(5,4,3,2)                 
      X = 3*A(2,I,1,K)                     
      A(1,2,3,4) = X + 1
Task: Phrase reversals



C#: using System;
using System.Linq;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            
            Func<string, string> reverse = s => new string(s.Reverse().ToArray());

            string phrase = "rosetta code phrase reversal";
            
            Console.WriteLine(reverse(phrase));
            
            Console.WriteLine(string.Join(" ", phrase.Split(' ').Select(word => reverse(word))));
            
            Console.WriteLine(string.Join(" ", phrase.Split(' ').Reverse()));
        }
    }
}









Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals

C: #include <stdio.h>
#include <string.h>



char* reverse_section(char *s, size_t length)
{
    if (length == 0) return s;

    size_t i; char temp;
    for (i = 0; i < length / 2 + 1; ++i)
        temp = s[i], s[i] = s[length - i], s[length - i] = temp;
    return s;
}

char* reverse_words_in_order(char *s, char delim)
{
    if (!strlen(s)) return s;

    size_t i, j;
    for (i = 0; i < strlen(s) - 1; ++i) {
        for (j = 0; s[i + j] != 0 && s[i + j] != delim; ++j)
            ;
        reverse_section(s + i, j - 1);
        s += j;
    }
    return s;
}

char* reverse_string(char *s)
{
    return strlen(s) ? reverse_section(s, strlen(s) - 1) : s;
}

char* reverse_order_of_words(char *s, char delim)
{
    reverse_string(s);
    reverse_words_in_order(s, delim);
    return s;
}

int main(void)
{
    char str[]    = "rosetta code phrase reversal";
    size_t lenstr = sizeof(str) / sizeof(str[0]);
    char scopy[lenstr];
    char delim = ' ';

    
    printf("Original:       \"%s\"\n", str);

    
    strncpy(scopy, str, lenstr);
    reverse_string(scopy);
    printf("Reversed:       \"%s\"\n", scopy);

    
    strncpy(scopy, str, lenstr);
    reverse_words_in_order(scopy, delim);
    printf("Reversed words: \"%s\"\n", scopy);

    
    strncpy(scopy, str, lenstr);
    reverse_order_of_words(scopy, delim);
    printf("Reversed order: \"%s\"\n", scopy);

    return 0;
}











Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals


C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>

int main() {
   std::string s = "rosetta code phrase reversal";
   std::cout << "Input : " << s << '\n'
             << "Input reversed : " << std::string(s.rbegin(), s.rend()) << '\n' ;
   std::istringstream is(s);
   std::vector<std::string> words(std::istream_iterator<std::string>(is), {});
   std::cout << "Each word reversed : " ;
   for(auto w : words)
      std::cout << std::string(w.rbegin(), w.rend()) << ' ';
   std::cout << '\n'
             << "Original word order reversed : " ;
   reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
   std::cout << '\n' ;
}










Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals




Go: package main

import (
	"fmt"
	"strings"
)

const phrase = "rosetta code phrase reversal"

func revStr(s string) string {
	rs := make([]rune, len(s))
	i := len(s)
	for _, r := range s {
		i--
		rs[i] = r
	}
	return string(rs[i:])
}

func main() {
	fmt.Println("Reversed:           ", revStr(phrase))

	ws := strings.Fields(phrase)
	for i, w := range ws {
		ws[i] = revStr(w)
	}
	fmt.Println("Words reversed:     ", strings.Join(ws, " "))

	ws = strings.Fields(phrase)
	last := len(ws) - 1
	for i, w := range ws[:len(ws)/2] {
		ws[i], ws[last-i] = ws[last-i], w
	}
	fmt.Println("Word order reversed:", strings.Join(ws, " "))
}








Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals





Java: import java.util.Arrays;

public class PhraseRev{
	private static String reverse(String x){
		return new StringBuilder(x).reverse().toString();
	}
	
	private static <T> T[] reverse(T[] x){
		T[] rev = Arrays.copyOf(x, x.length);
		for(int i = x.length - 1; i >= 0; i--){
			rev[x.length - 1 - i] = x[i];
		}
		return rev;
	}
	
	private static String join(String[] arr, String joinStr){
		StringBuilder joined = new StringBuilder();
		for(int i = 0; i < arr.length; i++){
			joined.append(arr[i]);
			if(i < arr.length - 1) joined.append(joinStr);
		}
		return joined.toString();
	}
	
	public static void main(String[] args){
		String str = "rosetta code phrase reversal";
		
		System.out.println("Straight-up reversed: " + reverse(str));
		String[] words = str.split(" ");
		for(int i = 0; i < words.length; i++){
			words[i] = reverse(words[i]);
		}
		System.out.println("Reversed words: " + join(words, " "));
		System.out.println("Reversed word order: " + join(reverse(str.split(" ")), " "));
	}
}







Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals







Python: >>> phrase = "rosetta code phrase reversal"
>>> phrase[::-1]					  
'lasrever esarhp edoc attesor'
>>> ' '.join(word[::-1] for word in phrase.split())	  
'attesor edoc esarhp lasrever'
>>> ' '.join(phrase.split()[::-1])	                  
'reversal phrase code rosetta'
>>>





Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals








VB: Option Explicit

Sub Main_Phrase_Reversals()
Const PHRASE As String = "rosetta code phrase reversal"
    Debug.Print "Original String              : " & PHRASE
    Debug.Print "Reverse String               : " & Reverse_String(PHRASE)
    Debug.Print "Reverse each individual word : " & Reverse_each_individual_word(PHRASE)
    Debug.Print "Reverse order of each word   : " & Reverse_the_order_of_each_word(PHRASE)
End Sub

Function Reverse_String(strPhrase As String) As String
    Reverse_String = StrReverse(strPhrase)
End Function

Function Reverse_each_individual_word(strPhrase As String) As String
Dim Words, i&, strTemp$
    Words = Split(strPhrase, " ")
    For i = 0 To UBound(Words)
        Words(i) = Reverse_String(CStr(Words(i)))
    Next i
    Reverse_each_individual_word = Join(Words, " ")
End Function

Function Reverse_the_order_of_each_word(strPhrase As String) As String
Dim Words, i&, strTemp$

    Words = Split(strPhrase, " ")
    For i = UBound(Words) To 0 Step -1
        strTemp = strTemp & " " & Words(i)
    Next i
    Reverse_the_order_of_each_word = Trim(strTemp)
End Function




Ada: <with Ada.Text_IO, Simple_Parse;

procedure Phrase_Reversal is
   
   function Reverse_String (Item : String) return String is
      Result : String (Item'Range);
   begin
      for I in Item'range loop
         Result (Result'Last - I + Item'First) := Item (I);
      end loop;
      return Result;
   end Reverse_String;
   
   function Reverse_Words(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Reverse_String(Word);
      else
	 return Reverse_String(Word) & " " & Reverse_Words(S(Cursor .. S'Last));
      end if;
   end Reverse_Words;
   
   function Reverse_Order(S: String) return String is
      Cursor: Positive := S'First;
      Word: String := Simple_Parse.Next_Word(S, Cursor);
   begin
      if Cursor > S'Last then 
	 return Word;
      else
	 return Reverse_Order(S(Cursor .. S'Last)) & " " & Word;
      end if;
   end Reverse_Order;
   
   Phrase: String := "rosetta code phrase reversal";
   use Ada.Text_IO;
begin
   Put_Line("0. The original phrase:       """ & Phrase & """");
   Put_Line("1. Reverse the entire phrase: """ & Reverse_String(Phrase) & """");
   Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
   Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;
Task: Phrase reversals

C: #include <stdio.h>
#include <string.h>



char* reverse_section(char *s, size_t length)
{
    if (length == 0) return s;

    size_t i; char temp;
    for (i = 0; i < length / 2 + 1; ++i)
        temp = s[i], s[i] = s[length - i], s[length - i] = temp;
    return s;
}

char* reverse_words_in_order(char *s, char delim)
{
    if (!strlen(s)) return s;

    size_t i, j;
    for (i = 0; i < strlen(s) - 1; ++i) {
        for (j = 0; s[i + j] != 0 && s[i + j] != delim; ++j)
            ;
        reverse_section(s + i, j - 1);
        s += j;
    }
    return s;
}

char* reverse_string(char *s)
{
    return strlen(s) ? reverse_section(s, strlen(s) - 1) : s;
}

char* reverse_order_of_words(char *s, char delim)
{
    reverse_string(s);
    reverse_words_in_order(s, delim);
    return s;
}

int main(void)
{
    char str[]    = "rosetta code phrase reversal";
    size_t lenstr = sizeof(str) / sizeof(str[0]);
    char scopy[lenstr];
    char delim = ' ';

    
    printf("Original:       \"%s\"\n", str);

    
    strncpy(scopy, str, lenstr);
    reverse_string(scopy);
    printf("Reversed:       \"%s\"\n", scopy);

    
    strncpy(scopy, str, lenstr);
    reverse_words_in_order(scopy, delim);
    printf("Reversed words: \"%s\"\n", scopy);

    
    strncpy(scopy, str, lenstr);
    reverse_order_of_words(scopy, delim);
    printf("Reversed order: \"%s\"\n", scopy);

    return 0;
}
Task: Phrase reversals



C#: using System;
using System.Linq;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            
            
            Func<string, string> reverse = s => new string(s.Reverse().ToArray());

            string phrase = "rosetta code phrase reversal";
            
            Console.WriteLine(reverse(phrase));
            
            Console.WriteLine(string.Join(" ", phrase.Split(' ').Select(word => reverse(word))));
            
            Console.WriteLine(string.Join(" ", phrase.Split(' ').Reverse()));
        }
    }
}
Task: Phrase reversals


C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>

int main() {
   std::string s = "rosetta code phrase reversal";
   std::cout << "Input : " << s << '\n'
             << "Input reversed : " << std::string(s.rbegin(), s.rend()) << '\n' ;
   std::istringstream is(s);
   std::vector<std::string> words(std::istream_iterator<std::string>(is), {});
   std::cout << "Each word reversed : " ;
   for(auto w : words)
      std::cout << std::string(w.rbegin(), w.rend()) << ' ';
   std::cout << '\n'
             << "Original word order reversed : " ;
   reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
   std::cout << '\n' ;
}
Task: Phrase reversals





Java: import java.util.Arrays;

public class PhraseRev{
	private static String reverse(String x){
		return new StringBuilder(x).reverse().toString();
	}
	
	private static <T> T[] reverse(T[] x){
		T[] rev = Arrays.copyOf(x, x.length);
		for(int i = x.length - 1; i >= 0; i--){
			rev[x.length - 1 - i] = x[i];
		}
		return rev;
	}
	
	private static String join(String[] arr, String joinStr){
		StringBuilder joined = new StringBuilder();
		for(int i = 0; i < arr.length; i++){
			joined.append(arr[i]);
			if(i < arr.length - 1) joined.append(joinStr);
		}
		return joined.toString();
	}
	
	public static void main(String[] args){
		String str = "rosetta code phrase reversal";
		
		System.out.println("Straight-up reversed: " + reverse(str));
		String[] words = str.split(" ");
		for(int i = 0; i < words.length; i++){
			words[i] = reverse(words[i]);
		}
		System.out.println("Reversed words: " + join(words, " "));
		System.out.println("Reversed word order: " + join(reverse(str.split(" ")), " "));
	}
}
Task: Phrase reversals







Python: >>> phrase = "rosetta code phrase reversal"
>>> phrase[::-1]					  
'lasrever esarhp edoc attesor'
>>> ' '.join(word[::-1] for word in phrase.split())	  
'attesor edoc esarhp lasrever'
>>> ' '.join(phrase.split()[::-1])	                  
'reversal phrase code rosetta'
>>>