WeixiangYan/CodeTransOcean huggingface dataset:
Task: Mian-Chowla sequence







Python: from itertools import count, islice, chain
import time

def mian_chowla():
    mc = [1]
    yield mc[-1]
    psums = set([2])
    newsums = set([])
    for trial in count(2):
        for n in chain(mc, [trial]):
            sum = n + trial
            if sum in psums:
                newsums.clear()
                break
            newsums.add(sum)
        else:
            psums |= newsums
            newsums.clear()
            mc.append(trial)
            yield trial

def pretty(p, t, s, f):
    print(p, t, " ".join(str(n) for n in (islice(mian_chowla(), s, f))))

if __name__ == '__main__':
    st = time.time()
    ts = "of the Mian-Chowla sequence are:\n"
    pretty("The first 30 terms", ts, 0, 30)
    pretty("\nTerms 91 to 100", ts, 90, 100)
    print("\nComputation time was", (time.time()-st) * 1000, "ms")


VB: 
    Const m = 100, mm=28000
    ReDim r(mm), v(mm * 2)
    Dim n, t, i, j, l, s1, s2, iterate_t
    ReDim seq(m)
    t0=Timer
    s1 = "1": s2 = ""
    seq(1) = 1: n = 1: t = 1
    Do While n < m
        t = t + 1
        iterate_t = False
        For i = 1 to t * 2
            v(i) = 0
        Next
        i = 1
        Do While i <= t And Not iterate_t
            If r(i) = 0 Then
                j = i
                Do While j <= t And Not iterate_t
                    If r(j) = 0 Then
                        l = i + j
                        If v(l) = 1 Then
                            r(t) = 1
                            iterate_t = True
                        End If
                        If Not iterate_t Then v(l) = 1
                    End If
                    j = j + 1
                Loop
            End If
            i = i + 1
        Loop
        If Not iterate_t Then
            n = n + 1
            seq(n) = t
            if           n<= 30 then s1 = s1 & " " & t
            if n>=91 and n<=100 then s2 = s2 & " " & t
        End If
    Loop
    wscript.echo "t="& t
    wscript.echo "The Mian-Chowla sequence for elements 1 to 30:"
    wscript.echo s1
    wscript.echo "The Mian-Chowla sequence for elements 91 to 100:"
    wscript.echo s2
    wscript.echo "Computation time: "&  Int(Timer-t0) &" sec"
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}



C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}




Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}







Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}



Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}
Task: Sudan function


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}






Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function



C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}


Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}
Task: Sudan function



C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}



Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}
Task: Sudan function



C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}





Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function




Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}


Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}
Task: Sudan function





Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}



Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function




Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}




Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}





Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}
Task: Sudan function


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}


C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}
Task: Sudan function


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}




Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}
Task: Sudan function

C: 

#include <stdio.h>

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  printf("F1(3,3) = %d",F(1,3,3));
  return 0;
}






PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>
Task: Sudan function



C#: 

using System;

namespace Sudan
{
  class Sudan
  {
  	static int F(int n,int x,int y) {
  		if (n == 0) {
    		return x + y;
  		}
 
  		else if (y == 0) {
    		return x;
  		}
 
  		return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
	}
    
    static void Main(string[] args)
    {
      Console.WriteLine("F(1,3,3) = " + F(1,3,3));    
    }
  }
}




PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>
Task: Sudan function


C++: 

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}





PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>
Task: Sudan function




Go: package main

import "fmt"

func F(n, x, y int) int {
    if n == 0 {
        return x + y
    }
    if y == 0 {
        return x
    }
    return F(n-1, F(n, x, y-1), F(n, x, y-1)+y)
}

func main() {
    for n := 0; n < 2; n++ {
        fmt.Printf("Values of F(%d, x, y):\n", n)
        fmt.Println("y/x    0   1   2   3   4   5")
        fmt.Println("----------------------------")
        for y := 0; y < 7; y++ {
            fmt.Printf("%d  |", y)
            for x := 0; x < 6; x++ {
                fmt.Printf("%4d", F(n, x, y))
            }
            fmt.Println()
        }
        fmt.Println()
    }
    fmt.Printf("F(2, 1, 1) = %d\n", F(2, 1, 1))
    fmt.Printf("F(3, 1, 1) = %d\n", F(3, 1, 1))
    fmt.Printf("F(2, 2, 1) = %d\n", F(2, 2, 1))
}



PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>
Task: Sudan function





Java: 

public class Main {

  private static int F(int n,int x,int y) {
  	if (n == 0) {
    	return x + y;
  	}
 
 	 else if (y == 0) {
    	return x;
  	}
 
  	return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
 }

  public static void main(String[] args) {
    System.out.println("F(1,3,3) = " + F(1,3,3));
  }
}


PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>
Task: Sudan function






PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>


Python: 

def F(n,x,y):
  if n==0:
    return x + y
  elif y==0:
    return x
  else:
    return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y)
    
    
print("F(1,3,3) = ", F(1,3,3))
Task: Sudan function






PHP: #Aamrun , 11th July 2022

<?php
function F(int $n,int $x,int $y) {
  if ($n == 0) {
    return $x + $y;
  }
 
  else if ($y == 0) {
    return $x;
  }
 
  return F($n - 1, F($n, $x, $y - 1), F($n, $x, $y - 1) + $y);
}
echo "F(1,3,3) = " . F(1,3,3); 
?>



VB: Function F(n As Integer, x As Integer, y As Integer) As Integer 
  
  If n = 0 Then Return x + y 
  If y = 0 Then Return x 
  Return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y) 
  
End Function

Public Sub Main() 
  
  Dim n, x, y As Integer

  For n = 0 To 1 
    Print " Values of F(" & n & ", x, y):" 
    Print " y/x    0   1   2   3   4   5" 
    Print String(29, "-") 
    For y = 0 To 6 
      Print y; "  | "; 
      For x = 0 To 5 
        Print Format$(F(n, x, y), "####"); 
      Next 
      Print 
    Next 
    Print 
  Next 
  
  Print "F(2,1,1) = "; F(2, 1, 1) 
  Print "F(3,1,1) = "; F(3, 1, 1) 
  Print "F(2,2,1) = "; F(2, 2, 1)
  
End
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}



C#: string src = "Hello";
string dst = src;
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}


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

int main( ) {
   std::string original ("This is the original");
   std::string my_copy = original;
   std::cout << "This is the copy: " << my_copy << std::endl;
   original = "Now we change the original! ";
   std::cout << "my_copy still is " << my_copy << std::endl;
}
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}




Go: src := "Hello"
dst := src
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}





Java: String src = "Hello";
String newAlias = src;
String strCopy = new String(src);
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}







Python: >>> src = "hello"
>>> a = src
>>> b = src[:]
>>> import copy
>>> c = copy.copy(src)
>>> d = copy.deepcopy(src)
>>> src is a is b is c is d
True
Task: Copy a string

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

int
main()
{
	size_t len;
	char src[] = "Hello";
	char dst1[80], dst2[80];
	char *dst3, *ref;

	
	strcpy(dst1, src);

	
	len = strlen(src);
	if (len >= sizeof dst2) {
		fputs("The buffer is too small!\n", stderr);
		exit(1);
	}
	memcpy(dst2, src, len + 1);

	
	dst3 = strdup(src);
	if (dst3 == NULL) {
		
		perror("strdup");
		exit(1);
	}

	
	ref = src;

	
	memset(src, '-', 5);

	printf(" src: %s\n", src);   
	printf("dst1: %s\n", dst1);  
	printf("dst2: %s\n", dst2);  
	printf("dst3: %s\n", dst3);  
	printf(" ref: %s\n", ref);   

	
	free(dst3);

	return 0;
}








VB: Sub copystring()
    a = "Hello World!"
    b = a
    a = "I
    Debug.Print b
    Debug.Print a
End Sub
Task: Copy a string


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

int main( ) {
   std::string original ("This is the original");
   std::string my_copy = original;
   std::cout << "This is the copy: " << my_copy << std::endl;
   original = "Now we change the original! ";
   std::cout << "my_copy still is " << my_copy << std::endl;
}


C#: string src = "Hello";
string dst = src;
Task: Copy a string


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

int main( ) {
   std::string original ("This is the original");
   std::string my_copy = original;
   std::cout << "This is the copy: " << my_copy << std::endl;
   original = "Now we change the original! ";
   std::cout << "my_copy still is " << my_copy << std::endl;
}



Go: src := "Hello"
dst := src
Task: Copy a string


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

int main( ) {
   std::string original ("This is the original");
   std::string my_copy = original;
   std::cout << "This is the copy: " << my_copy << std::endl;
   original = "Now we change the original! ";
   std::cout << "my_copy still is " << my_copy << std::endl;
}




Java: String src = "Hello";
String newAlias = src;
String strCopy = new String(src);