WeixiangYan/CodeTransOcean huggingface dataset:
Task: Substring







Python: >>> s = 'abcdefgh'
>>> n, m, char, chars = 2, 3, 'd', 'cd'
>>> 
>>> s[n-1:n+m-1]
'bcd'
>>> 
>>> s[n-1:]
'bcdefgh'
>>> 
>>> s[:-1]
'abcdefg'
>>> 
>>> indx = s.index(char)
>>> s[indx:indx+m]
'def'
>>> 
>>> indx = s.index(chars)
>>> s[indx:indx+m]
'cde'
>>>




Fortran: program test_substring

  character (*), parameter :: string = 'The quick brown fox jumps over the lazy dog.'
  character (*), parameter :: substring = 'brown'
  character    , parameter :: c = 'q'
  integer      , parameter :: n = 5
  integer      , parameter :: m = 15
  integer                  :: i


  write (*, '(a)') string (n : n + m - 1)

  write (*, '(a)') string (n :)

  i = len (string) - 1
  write (*, '(a)') string (: i)

  i = index (string, c)
  write (*, '(a)') string (i : i + m - 1)

  i = index (string, substring)
  write (*, '(a)') string (i : i + m - 1)

end program test_substring
Task: Substring






PHP: <?php
$str = 'abcdefgh';
$n = 2;
$m = 3;
echo substr($str, $n, $m), "\n"; //cde
echo substr($str, $n), "\n"; //cdefgh
echo substr($str, 0, -1), "\n"; //abcdefg
echo substr($str, strpos($str, 'd'), $m), "\n"; //def
echo substr($str, strpos($str, 'de'), $m), "\n"; //def
?>





Fortran: program test_substring

  character (*), parameter :: string = 'The quick brown fox jumps over the lazy dog.'
  character (*), parameter :: substring = 'brown'
  character    , parameter :: c = 'q'
  integer      , parameter :: n = 5
  integer      , parameter :: m = 15
  integer                  :: i


  write (*, '(a)') string (n : n + m - 1)

  write (*, '(a)') string (n :)

  i = len (string) - 1
  write (*, '(a)') string (: i)

  i = index (string, c)
  write (*, '(a)') string (i : i + m - 1)

  i = index (string, substring)
  write (*, '(a)') string (i : i + m - 1)

end program test_substring
Task: Substring

C: 
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void putm(char* string, size_t m)
{
    while(*string && m--)
        putchar(*string++);
}

int main(void)
{

    char string[] = 
        "Programs for other encodings (such as 8-bit ASCII, or EUC-JP)."

    int n = 3;
    int m = 4;
    char knownCharacter = '(';
    char knownSubstring[] = "encodings";

    putm(string+n-1, m );                       putchar('\n');
    puts(string+n+1);                           putchar('\n');
    putm(string, strlen(string)-1);             putchar('\n');
    putm(strchr(string, knownCharacter), m );   putchar('\n');
    putm(strstr(string, knownSubstring), m );   putchar('\n');

    return EXIT_SUCCESS;
}









COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring



C#: using System;
namespace SubString
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "0123456789";
            const int n = 3;
            const int m = 2;
            const char c = '3';
            const string z = "345";

            
            Console.WriteLine(s.Substring(n, m));
            
            Console.WriteLine(s.Substring(n, s.Length - n));
            
            Console.WriteLine(s.Substring(0, s.Length - 1));
            
            Console.WriteLine(s.Substring(s.IndexOf(c), m));
            
            Console.WriteLine(s.Substring(s.IndexOf(z), m));
        }
    }
}







COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring


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

int main()
{
  std::string s = "0123456789";

  int const n = 3;
  int const m = 4;
  char const c = '2';
  std::string const sub = "456";

  std::cout << s.substr(n, m)<< "\n";
  std::cout << s.substr(n) << "\n";
  std::cout << s.substr(0, s.size()-1) << "\n";
  std::cout << s.substr(s.find(c), m) << "\n";
  std::cout << s.substr(s.find(sub), m) << "\n";
}








COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring





Java: public static String Substring(String str, int n, int m){
    return str.substring(n, n+m);
}
public static String Substring(String str, int n){
    return str.substring(n);
}
public static String Substring(String str){
    return str.substring(0, str.length()-1);
}
public static String Substring(String str, char c, int m){
    return str.substring(str.indexOf(c), str.indexOf(c)+m+1);
}
public static String Substring(String str, String sub, int m){
    return str.substring(str.indexOf(sub), str.indexOf(sub)+m+1);
}





COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring







Python: >>> s = 'abcdefgh'
>>> n, m, char, chars = 2, 3, 'd', 'cd'
>>> 
>>> s[n-1:n+m-1]
'bcd'
>>> 
>>> s[n-1:]
'bcdefgh'
>>> 
>>> s[:-1]
'abcdefg'
>>> 
>>> indx = s.index(char)
>>> s[indx:indx+m]
'def'
>>> 
>>> indx = s.index(chars)
>>> s[indx:indx+m]
'cde'
>>>



COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring








VB: Public Sub substring()





 
    sentence = "the last thing the man said was the"
    n = 10: m = 5
       
    
    Debug.Print Mid(sentence, n, 5)
    
    Debug.Print Right(sentence, Len(sentence) - n + 1)
    
    Debug.Print Left(sentence, Len(sentence) - 1)
    
    k = InStr(1, sentence, "m")
    Debug.Print Mid(sentence, k, 5)
    
    k = InStr(1, sentence, "aid")
    Debug.Print Mid(sentence, k, 5)
End Sub


COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring




Go: package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "ABCDEFGH"
    n, m := 2, 3
    
    fmt.Println("Index: ", "01234567")
    fmt.Println("String:", s)
    
    fmt.Printf("Start %d, length %d:    %s\n", n, m, s[n : n+m])
    
    fmt.Printf("Start %d, to end:      %s\n", n, s[n:])
    
    fmt.Printf("All but last:         %s\n", s[:len(s)-1])
    
    dx := strings.IndexByte(s, 'D')
    fmt.Printf("Start 'D', length %d:  %s\n", m, s[dx : dx+m])
    
    sx := strings.Index(s, "DE")
    fmt.Printf(`Start "DE", length %d: %s`+"\n", m, s[sx : sx+m])
}






COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Substring






PHP: <?php
$str = 'abcdefgh';
$n = 2;
$m = 3;
echo substr($str, $n, $m), "\n"; //cde
echo substr($str, $n), "\n"; //cdefgh
echo substr($str, 0, -1), "\n"; //abcdefg
echo substr($str, strpos($str, 'd'), $m), "\n"; //def
echo substr($str, strpos($str, 'de'), $m), "\n"; //def
?>
Task: Substring






PHP: <?php
$str = 'abcdefgh';
$n = 2;
$m = 3;
echo substr($str, $n, $m), "\n"; //cde
echo substr($str, $n), "\n"; //cdefgh
echo substr($str, 0, -1), "\n"; //abcdefg
echo substr($str, strpos($str, 'd'), $m), "\n"; //def
echo substr($str, strpos($str, 'de'), $m), "\n"; //def
?>






Ada: type String is array (Positive range <>) of Character;
Task: Substring






PHP: <?php
$str = 'abcdefgh';
$n = 2;
$m = 3;
echo substr($str, $n, $m), "\n"; //cde
echo substr($str, $n), "\n"; //cdefgh
echo substr($str, 0, -1), "\n"; //abcdefg
echo substr($str, strpos($str, 'd'), $m), "\n"; //def
echo substr($str, strpos($str, 'de'), $m), "\n"; //def
?>




COBOL:        identification division.
       program-id. substring.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

       data division.
       working-storage section.
       01 original.
          05 value "this is a string".
       01 starting  pic 99 value 3.
       01 width     pic 99 value 8.
       01 pos       pic 99.
       01 ender     pic 99.
       01 looking   pic 99.
       01 indicator pic x.
          88 found  value high-value when set to false is low-value.
       01 look-for  pic x(8).

       procedure division.
       substring-main.

       display "Original |" original "|, n = " starting " m = " width
       display original(starting : width)
       display original(starting :)
       display original(1 : length(original) - 1)

       move "a" to look-for
       move 1 to looking
       perform find-position
       if found
           display original(pos : width)
       end-if

       move "is a st" to look-for
       move length(trim(look-for)) to looking
       perform find-position
       if found
           display original(pos : width)
       end-if
       goback.

       find-position.
       set found to false
       compute ender = length(original) - looking
       perform varying pos from 1 by 1 until pos > ender
           if original(pos : looking) equal look-for then
               set found to true
               exit perform
           end-if
       end-perform
       .

       end program substring.
Task: Evaluate binomial coefficients



C#: using System;

namespace BinomialCoefficients
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong n = 1000000, k = 3;
            ulong result = biCoefficient(n, k);
            Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result);
            Console.ReadLine();
        }

        static int fact(int n)
        {
            if (n == 0) return 1;
            else return n * fact(n - 1);
        }

        static ulong biCoefficient(ulong n, ulong k)
        {
            if (k > n - k)
            {
                k = n - k;
            }

            ulong c = 1;
            for (uint i = 0; i < k; i++)
            {
                c = c * (n - i);
                c = c / (i + 1);
            }
            return c;
        }
    }
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;
procedure Test_Binomial is
   function Binomial (N, K : Natural) return Natural is
      Result : Natural := 1;
      M      : Natural;
   begin
      if N < K then
         raise Constraint_Error;
      end if;
      if K > N/2 then 
         M := N - K;
      else
         M := K;
      end if;
      for I in 1..M loop
         Result := Result * (N - M + I) / I;
      end loop;
      return Result;
   end Binomial;
begin
   for N in 0..17 loop
      for K in 0..N loop
         Put (Integer'Image (Binomial (N, K)));
      end loop;
      New_Line;
   end loop;
end Test_Binomial;
Task: Evaluate binomial coefficients

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



static unsigned long gcd_ui(unsigned long x, unsigned long y) {
  unsigned long t;
  if (y < x) { t = x; x = y; y = t; }
  while (y > 0) {
    t = y;  y = x % y;  x = t;  
  }
  return x;
}

unsigned long binomial(unsigned long n, unsigned long k) {
  unsigned long d, g, r = 1;
  if (k == 0) return 1;
  if (k == 1) return n;
  if (k >= n) return (k == n);
  if (k > n/2) k = n-k;
  for (d = 1; d <= k; d++) {
    if (r >= ULONG_MAX/n) {  
      unsigned long nr, dr;  
      g = gcd_ui(n, d);  nr = n/g;  dr = d/g;
      g = gcd_ui(r, dr);  r = r/g;  dr = dr/g;
      if (r >= ULONG_MAX/nr) return 0;  
      r *= nr;
      r /= dr;
      n--;
    } else {
      r *= n--;
      r /= d;
    }
  }
  return r;
}

int main() {
    printf("%lu\n", binomial(5, 3));
    printf("%lu\n", binomial(40, 19));
    printf("%lu\n", binomial(67, 31));
    return 0;
}











Ada: with Ada.Text_IO;  use Ada.Text_IO;
procedure Test_Binomial is
   function Binomial (N, K : Natural) return Natural is
      Result : Natural := 1;
      M      : Natural;
   begin
      if N < K then
         raise Constraint_Error;
      end if;
      if K > N/2 then 
         M := N - K;
      else
         M := K;
      end if;
      for I in 1..M loop
         Result := Result * (N - M + I) / I;
      end loop;
      return Result;
   end Binomial;
begin
   for N in 0..17 loop
      for K in 0..N loop
         Put (Integer'Image (Binomial (N, K)));
      end loop;
      New_Line;
   end loop;
end Test_Binomial;
Task: Evaluate binomial coefficients


C++: double Factorial(double nValue)
   {
       double result = nValue;
       double result_next;
       double pc = nValue;
       do
       {
           result_next = result*(pc-1);
           result = result_next;
           pc--;
       }while(pc>2);
       nValue = result;
       return nValue;
   }

double binomialCoefficient(double n, double k)
   {
       if (abs(n - k) < 1e-7 || k  < 1e-7) return 1.0;
       if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n;
       return Factorial(n) /(Factorial(k)*Factorial((n - k)));
   }










Ada: with Ada.Text_IO;  use Ada.Text_IO;
procedure Test_Binomial is
   function Binomial (N, K : Natural) return Natural is
      Result : Natural := 1;
      M      : Natural;
   begin
      if N < K then
         raise Constraint_Error;
      end if;
      if K > N/2 then 
         M := N - K;
      else
         M := K;
      end if;
      for I in 1..M loop
         Result := Result * (N - M + I) / I;
      end loop;
      return Result;
   end Binomial;
begin
   for N in 0..17 loop
      for K in 0..N loop
         Put (Integer'Image (Binomial (N, K)));
      end loop;
      New_Line;
   end loop;
end Test_Binomial;
Task: Evaluate binomial coefficients




Go: package main
import "fmt"
import "math/big"

func main() {
  fmt.Println(new(big.Int).Binomial(5, 3))
  fmt.Println(new(big.Int).Binomial(60, 30))
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;
procedure Test_Binomial is
   function Binomial (N, K : Natural) return Natural is
      Result : Natural := 1;
      M      : Natural;
   begin
      if N < K then
         raise Constraint_Error;
      end if;
      if K > N/2 then 
         M := N - K;
      else
         M := K;
      end if;
      for I in 1..M loop
         Result := Result * (N - M + I) / I;
      end loop;
      return Result;
   end Binomial;
begin
   for N in 0..17 loop
      for K in 0..N loop
         Put (Integer'Image (Binomial (N, K)));
      end loop;
      New_Line;
   end loop;
end Test_Binomial;