WeixiangYan/CodeTransOcean huggingface dataset:
Task: Catalan numbers


C++: #if !defined __ALGORITHMS_H__
#define __ALGORITHMS_H__

namespace rosetta
  {
  namespace catalanNumbers
    {
    namespace detail
      {

      class Factorial
        {
        public:
          unsigned long long operator()(unsigned n)const;
        };

      class BinomialCoefficient
        {
        public:
          unsigned long long operator()(unsigned n, unsigned k)const;
        };

      } 

    class CatalanNumbersDirectFactorial
      {
      public:
        CatalanNumbersDirectFactorial();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::Factorial factorial;
      };

    class CatalanNumbersDirectBinomialCoefficient
      {
      public:
        CatalanNumbersDirectBinomialCoefficient();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::BinomialCoefficient binomialCoefficient;
      };

    class CatalanNumbersRecursiveSum
      {
      public:
        CatalanNumbersRecursiveSum();
        unsigned long long operator()(unsigned n)const;
      };

    class CatalanNumbersRecursiveFraction
      {
      public:
        CatalanNumbersRecursiveFraction();
        unsigned long long operator()(unsigned n)const;
      };

    }   
  }     

#endif 










Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Catalan is
   function Catalan (N : Natural) return Natural is
      Result : Positive := 1;
   begin
      for I in 1..N loop
         Result := Result * 2 * (2 * I - 1) / (I + 1);
      end loop;
      return Result;
   end Catalan;
begin
   for N in 0..15 loop
      Put_Line (Integer'Image (N) & " =" & Integer'Image (Catalan (N)));
   end loop;
end Test_Catalan;
Task: Catalan numbers




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var b, c big.Int
    for n := int64(0); n < 15; n++ {
        fmt.Println(c.Div(b.Binomial(n*2, n), c.SetInt64(n+1)))
    }
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Catalan is
   function Catalan (N : Natural) return Natural is
      Result : Positive := 1;
   begin
      for I in 1..N loop
         Result := Result * 2 * (2 * I - 1) / (I + 1);
      end loop;
      return Result;
   end Catalan;
begin
   for N in 0..15 loop
      Put_Line (Integer'Image (N) & " =" & Integer'Image (Catalan (N)));
   end loop;
end Test_Catalan;
Task: Catalan numbers







Python: from math import factorial
import functools


def memoize(func):
    cache = {}

    def memoized(key):
        
        if key not in cache:
            cache[key] = func(key)
        return cache[key]
    return functools.update_wrapper(memoized, func)


@memoize
def fact(n):
    return factorial(n)


def cat_direct(n):
    return fact(2 * n) // fact(n + 1) // fact(n)


@memoize
def catR1(n):
    return 1 if n == 0 else (
        sum(catR1(i) * catR1(n - 1 - i) for i in range(n))
    )


@memoize
def catR2(n):
    return 1 if n == 0 else (
        ((4 * n - 2) * catR2(n - 1)) // (n + 1)
    )


if __name__ == '__main__':
    def pr(results):
        fmt = '%-10s %-10s %-10s'
        print((fmt % tuple(c.__name__ for c in defs)).upper())
        print(fmt % (('=' * 10,) * 3))
        for r in zip(*results):
            print(fmt % r)

    defs = (cat_direct, catR1, catR2)
    results = [tuple(c(i) for i in range(15)) for c in defs]
    pr(results)





Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Catalan is
   function Catalan (N : Natural) return Natural is
      Result : Positive := 1;
   begin
      for I in 1..N loop
         Result := Result * 2 * (2 * I - 1) / (I + 1);
      end loop;
      return Result;
   end Catalan;
begin
   for N in 0..15 loop
      Put_Line (Integer'Image (N) & " =" & Integer'Image (Catalan (N)));
   end loop;
end Test_Catalan;
Task: Catalan numbers








VB: Public Sub Catalan1(n As Integer)

Dim Cat() As Long
Dim sum As Long

ReDim Cat(n)
Cat(0) = 1
For i = 0 To n - 1
  sum = 0
  For j = 0 To i
    sum = sum + Cat(j) * Cat(i - j)
  Next j
  Cat(i + 1) = sum
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub

Public Sub Catalan2(n As Integer)

Dim Cat() As Long

ReDim Cat(n)
Cat(0) = 1
For i = 1 To n
  Cat(i) = 2 * Cat(i - 1) * (2 * i - 1) / (i + 1)
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub




Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Catalan is
   function Catalan (N : Natural) return Natural is
      Result : Positive := 1;
   begin
      for I in 1..N loop
         Result := Result * 2 * (2 * I - 1) / (I + 1);
      end loop;
      return Result;
   end Catalan;
begin
   for N in 0..15 loop
      Put_Line (Integer'Image (N) & " =" & Integer'Image (Catalan (N)));
   end loop;
end Test_Catalan;
Task: Catalan numbers

C: #include <stdio.h>

typedef unsigned long long ull;

ull binomial(ull m, ull n)
{
	ull r = 1, d = m - n;
	if (d > n) { n = d; d = m - n; }

	while (m > n) {
		r *= m--;
		while (d > 1 && ! (r%d) ) r /= d--;
	}

	return r;
}

ull catalan1(int n) {
	return binomial(2 * n, n) / (1 + n);
}

ull catalan2(int n) {
	int i;
	ull r = !n;

	for (i = 0; i < n; i++)
		r += catalan2(i) * catalan2(n - 1 - i);
	return r;
}

ull catalan3(int n)
{
	return n ? 2 * (2 * n - 1) * catalan3(n - 1) / (1 + n) : 1;
}

int main(void)
{
	int i;
	puts("\tdirect\tsumming\tfrac");
	for (i = 0; i < 16; i++) {
		printf("%d\t%llu\t%llu\t%llu\n", i,
			catalan1(i), catalan2(i), catalan3(i));
	}

	return 0;
}
Task: Catalan numbers



C#: namespace CatalanNumbers
{
    
    
    
    public class CatalanNumberGenerator
    {
        private static double Factorial(double n)
        {
            if (n == 0)
                return 1;

            return n * Factorial(n - 1);
        }

        public double FirstOption(double n)
        {
            const double topMultiplier = 2;
            return Factorial(topMultiplier * n) / (Factorial(n + 1) * Factorial(n));
        }

        public double SecondOption(double n)
        {
            if (n == 0)
            {
                return 1;
            }
            double sum = 0;
            double i = 0;
            for (; i <= (n - 1); i++)
            {
                sum += SecondOption(i) * SecondOption((n - 1) - i);
            }
            return sum;
        }

        public double ThirdOption(double n)
        {
            if (n == 0)
            {
                return 1;
            }
            return ((2 * (2 * n - 1)) / (n + 1)) * ThirdOption(n - 1);
        }
    }
}



using System;
using System.Configuration;










namespace CatalanNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            CatalanNumberGenerator generator = new CatalanNumberGenerator();
            int i = 0;
            DateTime initial;
            DateTime final;
            TimeSpan ts;

            try
            {
                initial = DateTime.Now;
                for (; i <= Convert.ToInt32(ConfigurationManager.AppSettings["MaxCatalanNumber"]); i++)
                {
                    Console.WriteLine("CatalanNumber({0}):{1}", i, generator.FirstOption(i));
                }
                final = DateTime.Now;
                ts = final - initial;
                Console.WriteLine("It took {0}.{1} to execute\n", ts.Seconds, ts.Milliseconds);

                i = 0;
                initial = DateTime.Now;
                for (; i <= Convert.ToInt32(ConfigurationManager.AppSettings["MaxCatalanNumber"]); i++)
                {
                    Console.WriteLine("CatalanNumber({0}):{1}", i, generator.SecondOption(i));
                }
                final = DateTime.Now;
                ts = final - initial;
                Console.WriteLine("It took {0}.{1} to execute\n", ts.Seconds, ts.Milliseconds);   

                i = 0;
                initial = DateTime.Now;
                for (; i <= Convert.ToInt32(ConfigurationManager.AppSettings["MaxCatalanNumber"]); i++)
                {
                    Console.WriteLine("CatalanNumber({0}):{1}", i, generator.ThirdOption(i));
                }
                final = DateTime.Now;
                ts = final - initial;
                Console.WriteLine("It took {0}.{1} to execute", ts.Seconds, ts.Milliseconds, ts.TotalMilliseconds);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Stopped at index {0}:", i);
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}
Task: Catalan numbers


C++: #if !defined __ALGORITHMS_H__
#define __ALGORITHMS_H__

namespace rosetta
  {
  namespace catalanNumbers
    {
    namespace detail
      {

      class Factorial
        {
        public:
          unsigned long long operator()(unsigned n)const;
        };

      class BinomialCoefficient
        {
        public:
          unsigned long long operator()(unsigned n, unsigned k)const;
        };

      } 

    class CatalanNumbersDirectFactorial
      {
      public:
        CatalanNumbersDirectFactorial();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::Factorial factorial;
      };

    class CatalanNumbersDirectBinomialCoefficient
      {
      public:
        CatalanNumbersDirectBinomialCoefficient();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::BinomialCoefficient binomialCoefficient;
      };

    class CatalanNumbersRecursiveSum
      {
      public:
        CatalanNumbersRecursiveSum();
        unsigned long long operator()(unsigned n)const;
      };

    class CatalanNumbersRecursiveFraction
      {
      public:
        CatalanNumbersRecursiveFraction();
        unsigned long long operator()(unsigned n)const;
      };

    }   
  }     

#endif
Task: Catalan numbers







Python: from math import factorial
import functools


def memoize(func):
    cache = {}

    def memoized(key):
        
        if key not in cache:
            cache[key] = func(key)
        return cache[key]
    return functools.update_wrapper(memoized, func)


@memoize
def fact(n):
    return factorial(n)


def cat_direct(n):
    return fact(2 * n) // fact(n + 1) // fact(n)


@memoize
def catR1(n):
    return 1 if n == 0 else (
        sum(catR1(i) * catR1(n - 1 - i) for i in range(n))
    )


@memoize
def catR2(n):
    return 1 if n == 0 else (
        ((4 * n - 2) * catR2(n - 1)) // (n + 1)
    )


if __name__ == '__main__':
    def pr(results):
        fmt = '%-10s %-10s %-10s'
        print((fmt % tuple(c.__name__ for c in defs)).upper())
        print(fmt % (('=' * 10,) * 3))
        for r in zip(*results):
            print(fmt % r)

    defs = (cat_direct, catR1, catR2)
    results = [tuple(c(i) for i in range(15)) for c in defs]
    pr(results)
Task: Catalan numbers








VB: Public Sub Catalan1(n As Integer)

Dim Cat() As Long
Dim sum As Long

ReDim Cat(n)
Cat(0) = 1
For i = 0 To n - 1
  sum = 0
  For j = 0 To i
    sum = sum + Cat(j) * Cat(i - j)
  Next j
  Cat(i + 1) = sum
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub

Public Sub Catalan2(n As Integer)

Dim Cat() As Long

ReDim Cat(n)
Cat(0) = 1
For i = 1 To n
  Cat(i) = 2 * Cat(i - 1) * (2 * i - 1) / (i + 1)
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub
Task: Catalan numbers




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var b, c big.Int
    for n := int64(0); n < 15; n++ {
        fmt.Println(c.Div(b.Binomial(n*2, n), c.SetInt64(n+1)))
    }
}
Task: Catalan numbers


C++: #if !defined __ALGORITHMS_H__
#define __ALGORITHMS_H__

namespace rosetta
  {
  namespace catalanNumbers
    {
    namespace detail
      {

      class Factorial
        {
        public:
          unsigned long long operator()(unsigned n)const;
        };

      class BinomialCoefficient
        {
        public:
          unsigned long long operator()(unsigned n, unsigned k)const;
        };

      } 

    class CatalanNumbersDirectFactorial
      {
      public:
        CatalanNumbersDirectFactorial();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::Factorial factorial;
      };

    class CatalanNumbersDirectBinomialCoefficient
      {
      public:
        CatalanNumbersDirectBinomialCoefficient();
        unsigned long long operator()(unsigned n)const;
      private:
        detail::BinomialCoefficient binomialCoefficient;
      };

    class CatalanNumbersRecursiveSum
      {
      public:
        CatalanNumbersRecursiveSum();
        unsigned long long operator()(unsigned n)const;
      };

    class CatalanNumbersRecursiveFraction
      {
      public:
        CatalanNumbersRecursiveFraction();
        unsigned long long operator()(unsigned n)const;
      };

    }   
  }     

#endif 









Fortran: program main
  
  implicit none

  
  integer                      :: n

  
  double precision, external   :: catalan_numbers         
  
  

  write(*,'(1x,a)')'==============='
  write(*,'(5x,a,6x,a)')'n','c(n)'
  write(*,'(1x,a)')'---------------'

  do n = 0, 14
    write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
  enddo

  write(*,'(1x,a)')'==============='

  
end program main



double precision recursive function catalan_numbers(n) result(value)
  
  implicit none

  
  integer, intent(in)          :: n

  

  if ( n .eq. 0 ) then
    value = 1
  else 
    value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
  endif

  
end function catalan_numbers
Task: Catalan numbers

C: #include <stdio.h>

typedef unsigned long long ull;

ull binomial(ull m, ull n)
{
	ull r = 1, d = m - n;
	if (d > n) { n = d; d = m - n; }

	while (m > n) {
		r *= m--;
		while (d > 1 && ! (r%d) ) r /= d--;
	}

	return r;
}

ull catalan1(int n) {
	return binomial(2 * n, n) / (1 + n);
}

ull catalan2(int n) {
	int i;
	ull r = !n;

	for (i = 0; i < n; i++)
		r += catalan2(i) * catalan2(n - 1 - i);
	return r;
}

ull catalan3(int n)
{
	return n ? 2 * (2 * n - 1) * catalan3(n - 1) / (1 + n) : 1;
}

int main(void)
{
	int i;
	puts("\tdirect\tsumming\tfrac");
	for (i = 0; i < 16; i++) {
		printf("%d\t%llu\t%llu\t%llu\n", i,
			catalan1(i), catalan2(i), catalan3(i));
	}

	return 0;
}










Fortran: program main
  
  implicit none

  
  integer                      :: n

  
  double precision, external   :: catalan_numbers         
  
  

  write(*,'(1x,a)')'==============='
  write(*,'(5x,a,6x,a)')'n','c(n)'
  write(*,'(1x,a)')'---------------'

  do n = 0, 14
    write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
  enddo

  write(*,'(1x,a)')'==============='

  
end program main



double precision recursive function catalan_numbers(n) result(value)
  
  implicit none

  
  integer, intent(in)          :: n

  

  if ( n .eq. 0 ) then
    value = 1
  else 
    value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
  endif

  
end function catalan_numbers
Task: Catalan numbers







Python: from math import factorial
import functools


def memoize(func):
    cache = {}

    def memoized(key):
        
        if key not in cache:
            cache[key] = func(key)
        return cache[key]
    return functools.update_wrapper(memoized, func)


@memoize
def fact(n):
    return factorial(n)


def cat_direct(n):
    return fact(2 * n) // fact(n + 1) // fact(n)


@memoize
def catR1(n):
    return 1 if n == 0 else (
        sum(catR1(i) * catR1(n - 1 - i) for i in range(n))
    )


@memoize
def catR2(n):
    return 1 if n == 0 else (
        ((4 * n - 2) * catR2(n - 1)) // (n + 1)
    )


if __name__ == '__main__':
    def pr(results):
        fmt = '%-10s %-10s %-10s'
        print((fmt % tuple(c.__name__ for c in defs)).upper())
        print(fmt % (('=' * 10,) * 3))
        for r in zip(*results):
            print(fmt % r)

    defs = (cat_direct, catR1, catR2)
    results = [tuple(c(i) for i in range(15)) for c in defs]
    pr(results)




Fortran: program main
  
  implicit none

  
  integer                      :: n

  
  double precision, external   :: catalan_numbers         
  
  

  write(*,'(1x,a)')'==============='
  write(*,'(5x,a,6x,a)')'n','c(n)'
  write(*,'(1x,a)')'---------------'

  do n = 0, 14
    write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
  enddo

  write(*,'(1x,a)')'==============='

  
end program main



double precision recursive function catalan_numbers(n) result(value)
  
  implicit none

  
  integer, intent(in)          :: n

  

  if ( n .eq. 0 ) then
    value = 1
  else 
    value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
  endif

  
end function catalan_numbers
Task: Catalan numbers








VB: Public Sub Catalan1(n As Integer)

Dim Cat() As Long
Dim sum As Long

ReDim Cat(n)
Cat(0) = 1
For i = 0 To n - 1
  sum = 0
  For j = 0 To i
    sum = sum + Cat(j) * Cat(i - j)
  Next j
  Cat(i + 1) = sum
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub

Public Sub Catalan2(n As Integer)

Dim Cat() As Long

ReDim Cat(n)
Cat(0) = 1
For i = 1 To n
  Cat(i) = 2 * Cat(i - 1) * (2 * i - 1) / (i + 1)
Next i
Debug.Print
For i = 0 To n
  Debug.Print i, Cat(i)
Next
End Sub



Fortran: program main
  
  implicit none

  
  integer                      :: n

  
  double precision, external   :: catalan_numbers         
  
  

  write(*,'(1x,a)')'==============='
  write(*,'(5x,a,6x,a)')'n','c(n)'
  write(*,'(1x,a)')'---------------'

  do n = 0, 14
    write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
  enddo

  write(*,'(1x,a)')'==============='

  
end program main



double precision recursive function catalan_numbers(n) result(value)
  
  implicit none

  
  integer, intent(in)          :: n

  

  if ( n .eq. 0 ) then
    value = 1
  else 
    value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
  endif

  
end function catalan_numbers
Task: Catalan numbers






PHP: <?php

class CatalanNumbersSerie
{
  private static $cache = array(0 => 1);
   
  private function fill_cache($i)
  {
    $accum = 0;
    $n = $i-1;
    for($k = 0; $k <= $n; $k++)
    {
      $accum += $this->item($k)*$this->item($n-$k);
    } 
    self::$cache[$i] = $accum;
  }
  function item($i)
  {
    if (!isset(self::$cache[$i]))
    {
      $this->fill_cache($i);
    }
    return self::$cache[$i];
  }
}

$cn = new CatalanNumbersSerie();
for($i = 0; $i <= 15;$i++)
{
  $r = $cn->item($i);
  echo "$i = $r\r\n";
}
?>





Fortran: program main
  
  implicit none

  
  integer                      :: n

  
  double precision, external   :: catalan_numbers         
  
  

  write(*,'(1x,a)')'==============='
  write(*,'(5x,a,6x,a)')'n','c(n)'
  write(*,'(1x,a)')'---------------'

  do n = 0, 14
    write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
  enddo

  write(*,'(1x,a)')'==============='

  
end program main



double precision recursive function catalan_numbers(n) result(value)
  
  implicit none

  
  integer, intent(in)          :: n

  

  if ( n .eq. 0 ) then
    value = 1
  else 
    value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
  endif

  
end function catalan_numbers
Task: Catalan numbers






PHP: <?php

class CatalanNumbersSerie
{
  private static $cache = array(0 => 1);
   
  private function fill_cache($i)
  {
    $accum = 0;
    $n = $i-1;
    for($k = 0; $k <= $n; $k++)
    {
      $accum += $this->item($k)*$this->item($n-$k);
    } 
    self::$cache[$i] = $accum;
  }
  function item($i)
  {
    if (!isset(self::$cache[$i]))
    {
      $this->fill_cache($i);
    }
    return self::$cache[$i];
  }
}

$cn = new CatalanNumbersSerie();
for($i = 0; $i <= 15;$i++)
{
  $r = $cn->item($i);
  echo "$i = $r\r\n";
}
?>
Task: Catalan numbers






PHP: <?php

class CatalanNumbersSerie
{
  private static $cache = array(0 => 1);
   
  private function fill_cache($i)
  {
    $accum = 0;
    $n = $i-1;
    for($k = 0; $k <= $n; $k++)
    {
      $accum += $this->item($k)*$this->item($n-$k);
    } 
    self::$cache[$i] = $accum;
  }
  function item($i)
  {
    if (!isset(self::$cache[$i]))
    {
      $this->fill_cache($i);
    }
    return self::$cache[$i];
  }
}

$cn = new CatalanNumbersSerie();
for($i = 0; $i <= 15;$i++)
{
  $r = $cn->item($i);
  echo "$i = $r\r\n";
}
?>






Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Catalan is
   function Catalan (N : Natural) return Natural is
      Result : Positive := 1;
   begin
      for I in 1..N loop
         Result := Result * 2 * (2 * I - 1) / (I + 1);
      end loop;
      return Result;
   end Catalan;
begin
   for N in 0..15 loop
      Put_Line (Integer'Image (N) & " =" & Integer'Image (Catalan (N)));
   end loop;
end Test_Catalan;
Task: Reverse a string



C#: using System;

namespace System
{
	extension String
	{
		public void Reverse()
		{
			int i = 0;
			int j = mLength - 1;
			while (i < j)
			{
				Swap!(Ptr[i++], Ptr[j--]);
			}
		}
	}
}

namespace StringReverse
{
	class Program
	{
		static void Main()
		{
			String s = scope .("abcdef");
			s.Reverse();
			Console.WriteLine(s);
		}
	}
}









Ada: with Ada.Text_IO; use Ada.Text_IO;
 
procedure Reverse_String is
   function Reverse_It (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_It;
begin
   Put_Line (Reverse_It (Get_Line));
end Reverse_String;
Task: Reverse a string

C: #include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>

const char *sa = "abcdef";
const char *su = "as⃝df̅"; 

int is_comb(wchar_t c)
{
	if (c >= 0x300 && c <= 0x36f) return 1;
	if (c >= 0x1dc0 && c <= 0x1dff) return 1;
	if (c >= 0x20d0 && c <= 0x20ff) return 1;
	if (c >= 0xfe20 && c <= 0xfe2f) return 1;
	return 0;
}

wchar_t* mb_to_wchar(const char *s)
{
	wchar_t *u;
	size_t len = mbstowcs(0, s, 0) + 1;
	if (!len) return 0;

	u = malloc(sizeof(wchar_t) * len);
	mbstowcs(u, s, len);
	return u;
}

wchar_t* ws_reverse(const wchar_t* u)
{
	size_t len, i, j;
	wchar_t *out;
	for (len = 0; u[len]; len++);
	out = malloc(sizeof(wchar_t) * (len + 1));
	out[len] = 0;
	j = 0;
	while (len) {
		for (i = len - 1; i && is_comb(u[i]); i--);
		wcsncpy(out + j, u + i, len - i);
		j += len - i;
		len = i;
	}
	return out;
}

char *mb_reverse(const char *in)
{
	size_t len;
	char *out;
	wchar_t *u = mb_to_wchar(in);
	wchar_t *r = ws_reverse(u);
	len = wcstombs(0, r, 0) + 1;
	out = malloc(len);
	wcstombs(out, r, len);
	free(u);
	free(r);
	return out;
}

int main(void)
{
	setlocale(LC_CTYPE, "");

	printf("%s => %s\n", sa, mb_reverse(sa));
	printf("%s => %s\n", su, mb_reverse(su));
	return 0;
}











Ada: with Ada.Text_IO; use Ada.Text_IO;
 
procedure Reverse_String is
   function Reverse_It (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_It;
begin
   Put_Line (Reverse_It (Get_Line));
end Reverse_String;
Task: Reverse a string


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

int main()
{
  std::string s;
  std::getline(std::cin, s);
  std::reverse(s.begin(), s.end()); 
  std::cout << s << std::endl;
  return 0;
}










Ada: with Ada.Text_IO; use Ada.Text_IO;
 
procedure Reverse_String is
   function Reverse_It (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_It;
begin
   Put_Line (Reverse_It (Get_Line));
end Reverse_String;