WeixiangYan/CodeTransOcean huggingface dataset:
Task: Reverse words in a string

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

void rev_print(char *s, int n)
{
        for (; *s && isspace(*s); s++);
        if (*s) {
                char *e;
                for (e = s; *e && !isspace(*e); e++);
                rev_print(e, 0);
                printf("%.*s%s", (int)(e - s), s, " " + n);
        }
        if (n) putchar('\n');
}

int main(void)
{
        char *s[] = {
                "---------- Ice and Fire ------------",
                "                                    ",
                "fire, in end will world the say Some",
                "ice. in say Some                    ",
                "desire of tasted I've what From     ",
                "fire. favor who those with hold I   ",
                "                                    ",
                "... elided paragraph last ...       ",
                "                                    ",
                "Frost Robert -----------------------",
                0
        };
        int i;
        for (i = 0; s[i]; i++) rev_print(s[i], 1);

        return 0;
}











Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string


C++: #include <algorithm>
#include <functional>
#include <string>
#include <iostream>
#include <vector>


template <class BidirectionalIterator, class T>
void block_reverse_cpp11(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   auto block_last = first;
   do {
      using std::placeholders::_1;
      auto block_first = std::find_if_not(block_last, last, 
         std::bind(std::equal_to<T>(),_1, separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}


template <class BidirectionalIterator, class T>
void block_reverse_cpp03(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   BidirectionalIterator block_last = first;
   do {
      BidirectionalIterator block_first = std::find_if(block_last, last, 
         std::bind2nd(std::not_equal_to<T>(), separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}

int main() {
   std::string str1[] = 
    {
        "---------- Ice and Fire ------------",
        "",
        "fire, in end will world the say Some",
        "ice. in say Some",
        "desire of tasted I've what From",
        "fire. favor who those with hold I",
        "",
        "... elided paragraph last ...",
        "",
        "Frost Robert -----------------------"
    };

   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp11(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });
   
   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp03(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });

   return 0;
}










Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string




Go: package main

import (
    "fmt"
    "strings"
)


var n = []string{
    "---------- Ice and Fire ------------",
    "                                    ",
    "fire, in end will world the say Some",
    "ice. in say Some                    ",
    "desire of tasted I've what From     ",
    "fire. favor who those with hold I   ",
    "                                    ",
    "... elided paragraph last ...       ",
    "                                    ",
    "Frost Robert -----------------------",
}

func main() {
    for i, s := range n {
        t := strings.Fields(s) 
        
        last := len(t) - 1
        for j, k := range t[:len(t)/2] {
            t[j], t[last-j] = t[last-j], k
        }
        n[i] = strings.Join(t, " ")
    }
    
    for _, t := range n {
        fmt.Println(t)
    }
}








Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string





Java: public class ReverseWords {

    static final String[] lines = {
        " ----------- Ice and Fire ----------- ",
        "                                      ",
        " fire, in end will world the say Some ",
        " ice. in say Some                     ",
        " desire of tasted I've what From      ",
        " fire. favor who those with hold I    ",
        "                                      ",
        " ... elided paragraph last ...        ",
        " Frost Robert ----------------------- "};

    public static void main(String[] args) {
        for (String line : lines) {
            String[] words = line.split("\\s");
            for (int i = words.length - 1; i >= 0; i--)
                System.out.printf("%s ", words[i]);
            System.out.println();
        }
    }
}







Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string







Python:  text = 

for line in text.split('\n'): print(' '.join(line.split()[::-1]))





Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string








VB: Option Explicit

Sub Main()
Dim Lines(9) As String, i&
    
    Lines(0) = "------------- Ice And Fire -------------"
    Lines(1) = ""
    Lines(2) = "fire, in end will world the say Some"
    Lines(3) = "ice. in say Some"
    Lines(4) = "desire of tasted I
    Lines(5) = "fire. favor who those with hold I"
    Lines(6) = ""
    Lines(7) = "... elided paragraph last ..."
    Lines(8) = ""
    Lines(9) = "Frost Robert -----------------------"
    
    For i = 0 To 9
        Debug.Print ReverseLine(Lines(i), " ")
    Next
End Sub

Private Function ReverseLine(Line As String, Optional Separat As String) As String
Dim T, R, i&, j&, deb&, fin&
    If Len(Line) = 0 Then
        ReverseLine = vbNullString
    Else
        If Separat = "" Then Separat = " "
        T = Split(Line, Separat)
        ReDim R(UBound(T)): j = LBound(T)
        deb = UBound(T): fin = deb / 2
        For i = deb To fin Step -1
            R(j) = T(i)
            R(i) = T(j)
            j = j + 1
        Next i
        ReverseLine = Join(R, Separat)
    End If
End Function




Ada: package Simple_Parse is
   
   
   function Next_Word(S: String; Point: in out Positive) 
		     return String;
   
   
   
   
      
end Simple_Parse;
Task: Reverse words in a string

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

void rev_print(char *s, int n)
{
        for (; *s && isspace(*s); s++);
        if (*s) {
                char *e;
                for (e = s; *e && !isspace(*e); e++);
                rev_print(e, 0);
                printf("%.*s%s", (int)(e - s), s, " " + n);
        }
        if (n) putchar('\n');
}

int main(void)
{
        char *s[] = {
                "---------- Ice and Fire ------------",
                "                                    ",
                "fire, in end will world the say Some",
                "ice. in say Some                    ",
                "desire of tasted I've what From     ",
                "fire. favor who those with hold I   ",
                "                                    ",
                "... elided paragraph last ...       ",
                "                                    ",
                "Frost Robert -----------------------",
                0
        };
        int i;
        for (i = 0; s[i]; i++) rev_print(s[i], 1);

        return 0;
}
Task: Reverse words in a string



C#: using System;

public class ReverseWordsInString
{
    public static void Main(string[] args)
    {
        string text = @"
            ---------- Ice and Fire ------------

            fire, in end will world the say Some
            ice. in say Some
            desire of tasted I've what From
            fire. favor who those with hold I

            ... elided paragraph last ...

            Frost Robert -----------------------
            ";

        foreach (string line in text.Split(Environment.NewLine)) {
            
            string[] words = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
            Array.Reverse(words);
            WriteLine(string.Join(" ", words));
        }
    }
}
Task: Reverse words in a string


C++: #include <algorithm>
#include <functional>
#include <string>
#include <iostream>
#include <vector>


template <class BidirectionalIterator, class T>
void block_reverse_cpp11(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   auto block_last = first;
   do {
      using std::placeholders::_1;
      auto block_first = std::find_if_not(block_last, last, 
         std::bind(std::equal_to<T>(),_1, separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}


template <class BidirectionalIterator, class T>
void block_reverse_cpp03(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   BidirectionalIterator block_last = first;
   do {
      BidirectionalIterator block_first = std::find_if(block_last, last, 
         std::bind2nd(std::not_equal_to<T>(), separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}

int main() {
   std::string str1[] = 
    {
        "---------- Ice and Fire ------------",
        "",
        "fire, in end will world the say Some",
        "ice. in say Some",
        "desire of tasted I've what From",
        "fire. favor who those with hold I",
        "",
        "... elided paragraph last ...",
        "",
        "Frost Robert -----------------------"
    };

   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp11(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });
   
   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp03(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });

   return 0;
}
Task: Reverse words in a string





Java: public class ReverseWords {

    static final String[] lines = {
        " ----------- Ice and Fire ----------- ",
        "                                      ",
        " fire, in end will world the say Some ",
        " ice. in say Some                     ",
        " desire of tasted I've what From      ",
        " fire. favor who those with hold I    ",
        "                                      ",
        " ... elided paragraph last ...        ",
        " Frost Robert ----------------------- "};

    public static void main(String[] args) {
        for (String line : lines) {
            String[] words = line.split("\\s");
            for (int i = words.length - 1; i >= 0; i--)
                System.out.printf("%s ", words[i]);
            System.out.println();
        }
    }
}
Task: Reverse words in a string







Python:  text = 

for line in text.split('\n'): print(' '.join(line.split()[::-1]))
Task: Reverse words in a string








VB: Option Explicit

Sub Main()
Dim Lines(9) As String, i&
    
    Lines(0) = "------------- Ice And Fire -------------"
    Lines(1) = ""
    Lines(2) = "fire, in end will world the say Some"
    Lines(3) = "ice. in say Some"
    Lines(4) = "desire of tasted I
    Lines(5) = "fire. favor who those with hold I"
    Lines(6) = ""
    Lines(7) = "... elided paragraph last ..."
    Lines(8) = ""
    Lines(9) = "Frost Robert -----------------------"
    
    For i = 0 To 9
        Debug.Print ReverseLine(Lines(i), " ")
    Next
End Sub

Private Function ReverseLine(Line As String, Optional Separat As String) As String
Dim T, R, i&, j&, deb&, fin&
    If Len(Line) = 0 Then
        ReverseLine = vbNullString
    Else
        If Separat = "" Then Separat = " "
        T = Split(Line, Separat)
        ReDim R(UBound(T)): j = LBound(T)
        deb = UBound(T): fin = deb / 2
        For i = deb To fin Step -1
            R(j) = T(i)
            R(i) = T(j)
            j = j + 1
        Next i
        ReverseLine = Join(R, Separat)
    End If
End Function
Task: Reverse words in a string




Go: package main

import (
    "fmt"
    "strings"
)


var n = []string{
    "---------- Ice and Fire ------------",
    "                                    ",
    "fire, in end will world the say Some",
    "ice. in say Some                    ",
    "desire of tasted I've what From     ",
    "fire. favor who those with hold I   ",
    "                                    ",
    "... elided paragraph last ...       ",
    "                                    ",
    "Frost Robert -----------------------",
}

func main() {
    for i, s := range n {
        t := strings.Fields(s) 
        
        last := len(t) - 1
        for j, k := range t[:len(t)/2] {
            t[j], t[last-j] = t[last-j], k
        }
        n[i] = strings.Join(t, " ")
    }
    
    for _, t := range n {
        fmt.Println(t)
    }
}
Task: Reverse words in a string



C#: using System;

public class ReverseWordsInString
{
    public static void Main(string[] args)
    {
        string text = @"
            ---------- Ice and Fire ------------

            fire, in end will world the say Some
            ice. in say Some
            desire of tasted I've what From
            fire. favor who those with hold I

            ... elided paragraph last ...

            Frost Robert -----------------------
            ";

        foreach (string line in text.Split(Environment.NewLine)) {
            
            string[] words = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
            Array.Reverse(words);
            WriteLine(string.Join(" ", words));
        }
    }
}








Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end
Task: Reverse words in a string


C++: #include <algorithm>
#include <functional>
#include <string>
#include <iostream>
#include <vector>


template <class BidirectionalIterator, class T>
void block_reverse_cpp11(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   auto block_last = first;
   do {
      using std::placeholders::_1;
      auto block_first = std::find_if_not(block_last, last, 
         std::bind(std::equal_to<T>(),_1, separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}


template <class BidirectionalIterator, class T>
void block_reverse_cpp03(BidirectionalIterator first, BidirectionalIterator last, T const& separator) {
   std::reverse(first, last);
   BidirectionalIterator block_last = first;
   do {
      BidirectionalIterator block_first = std::find_if(block_last, last, 
         std::bind2nd(std::not_equal_to<T>(), separator));
      block_last = std::find(block_first, last, separator);
      std::reverse(block_first, block_last);
   } while(block_last != last);
}

int main() {
   std::string str1[] = 
    {
        "---------- Ice and Fire ------------",
        "",
        "fire, in end will world the say Some",
        "ice. in say Some",
        "desire of tasted I've what From",
        "fire. favor who those with hold I",
        "",
        "... elided paragraph last ...",
        "",
        "Frost Robert -----------------------"
    };

   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp11(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });
   
   std::for_each(begin(str1), end(str1), [](std::string& s){
      block_reverse_cpp03(begin(s), end(s), ' ');
      std::cout << s << std::endl;
   });

   return 0;
}









Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end
Task: Reverse words in a string

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

void rev_print(char *s, int n)
{
        for (; *s && isspace(*s); s++);
        if (*s) {
                char *e;
                for (e = s; *e && !isspace(*e); e++);
                rev_print(e, 0);
                printf("%.*s%s", (int)(e - s), s, " " + n);
        }
        if (n) putchar('\n');
}

int main(void)
{
        char *s[] = {
                "---------- Ice and Fire ------------",
                "                                    ",
                "fire, in end will world the say Some",
                "ice. in say Some                    ",
                "desire of tasted I've what From     ",
                "fire. favor who those with hold I   ",
                "                                    ",
                "... elided paragraph last ...       ",
                "                                    ",
                "Frost Robert -----------------------",
                0
        };
        int i;
        for (i = 0; s[i]; i++) rev_print(s[i], 1);

        return 0;
}










Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end
Task: Reverse words in a string





Java: public class ReverseWords {

    static final String[] lines = {
        " ----------- Ice and Fire ----------- ",
        "                                      ",
        " fire, in end will world the say Some ",
        " ice. in say Some                     ",
        " desire of tasted I've what From      ",
        " fire. favor who those with hold I    ",
        "                                      ",
        " ... elided paragraph last ...        ",
        " Frost Robert ----------------------- "};

    public static void main(String[] args) {
        for (String line : lines) {
            String[] words = line.split("\\s");
            for (int i = words.length - 1; i >= 0; i--)
                System.out.printf("%s ", words[i]);
            System.out.println();
        }
    }
}






Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end
Task: Reverse words in a string







Python:  text = 

for line in text.split('\n'): print(' '.join(line.split()[::-1]))




Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end
Task: Reverse words in a string






PHP:  
<?php
 
 function strInv ($string) {

 	$str_inv = '' ; 

	 for ($i=0,$s=count($string);$i<$s;$i++){
	 	$str_inv .= implode(' ',array_reverse(explode(' ',$string[$i])));
	 	$str_inv .= '<br>';
	 }

 	return $str_inv;

 }
  
 $string[] =  "---------- Ice and Fire ------------";
 $string[] =  "";
 $string[] =  "fire, in end will world the say Some";
 $string[] =  "ice. in say Some";
 $string[] =  "desire of tasted I've what From";
 $string[] =  "fire. favor who those with hold I";
 $string[] =  "";
 $string[] =  "... elided paragraph last ...";
 $string[] =  "";
 $string[] =  "Frost Robert ----------------------- ";


echo strInv($string);





Fortran:  character*40 words
 character*40 reversed
 logical inblank
 ierr=0
 read (5,fmt="(a)",iostat=ierr)words
 do while (ierr.eq.0)
 inblank=.true.
 ipos=1
 do i=40,1,-1
   if(words(i:i).ne.' '.and.inblank) then
     last=i
     inblank=.false.
     end if
    if(.not.inblank.and.words(i:i).eq.' ') then
      reversed(ipos:ipos+last-i)=words(i+1:last)
      ipos=ipos+last-i+1
      inblank=.true.
      end if
     if(.not.inblank.and.i.eq.1) then
       reversed(ipos:ipos+last-1)=words(1:last)
       ipos=ipos+last
       end if
   end do
 print *,words,'=> ',reversed(1:ipos-1)
 read (5,fmt="(a)",iostat=ierr)words
 end do
 end