WeixiangYan/CodeTransOcean huggingface dataset:
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
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, " "))
}
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()));
        }
    }
}








Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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' ;
}









Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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;
}










Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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(" ")), " "));
	}
}






Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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'
>>>




Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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



Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
Task: Phrase reversals






PHP: <?php

$strin = "rosetta code phrase reversal";

echo "Input: ".$strin."\n";

echo "Reversed: ".strrev($strin)."\n";

$str_words_reversed = "";
$temp = explode(" ", $strin);
foreach($temp as $word)
	$str_words_reversed .= strrev($word)." ";

echo "Words reversed: ".$str_words_reversed."\n";

$str_word_order_reversed = "";
$temp = explode(" ", $strin);
for($i=(count($temp)-1); $i>=0; $i--)
	$str_word_order_reversed .= $temp[$i]." ";

echo "Word order reversed: ".$str_word_order_reversed."\n";





Fortran:       DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
        L1 = L1 + 1
      END DO
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;
}









COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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()));
        }
    }
}







COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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' ;
}








COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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(" ")), " "));
	}
}





COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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'
>>>



COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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


COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
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, " "))
}






COBOL:        program-id. phra-rev.
       data division.
       working-storage section.
       1 phrase pic x(28) value "rosetta code phrase reversal".
       1 wk-str pic x(16).
       1 binary.
        2 phrase-len pic 9(4).
        2 pos pic 9(4).
        2 cnt pic 9(4).
       procedure division.
           compute phrase-len = function length (phrase)
           display phrase
           display function reverse (phrase)
           perform display-words
           move function reverse (phrase) to phrase
           perform display-words
           stop run
           .

       display-words.
           move 1 to pos
           perform until pos > phrase-len
               unstring phrase delimited space
               into wk-str count in cnt
               with pointer pos
               end-unstring
               display function reverse (wk-str (1:cnt))
                   with no advancing
               if pos < phrase-len
                   display space with no advancing
               end-if
           end-perform
           display space
           .
       end program phra-rev.
Task: Phrase reversals






PHP: <?php

$strin = "rosetta code phrase reversal";

echo "Input: ".$strin."\n";

echo "Reversed: ".strrev($strin)."\n";

$str_words_reversed = "";
$temp = explode(" ", $strin);
foreach($temp as $word)
	$str_words_reversed .= strrev($word)." ";

echo "Words reversed: ".$str_words_reversed."\n";

$str_word_order_reversed = "";
$temp = explode(" ", $strin);
for($i=(count($temp)-1); $i>=0; $i--)
	$str_word_order_reversed .= $temp[$i]." ";

echo "Word order reversed: ".$str_word_order_reversed."\n";