WeixiangYan/CodeTransOcean huggingface dataset:
Task: Soloway's recurring rainfall



C#: namespace RosettaCode
{
	class CSharpRecurringRainfall
    {		
		static int ReadNextInput()
		{
			System.Console.Write("Enter rainfall int, 99999 to quit: ");
			string input = System.Console.ReadLine();
			
			if (System.Int32.TryParse(input, out int num))
			{
				return num;
			}
			else
			{
				System.Console.WriteLine("Invalid input");
				return ReadNextInput();
			}
		}
		
        static void Main()
        {
			double currentAverage = 0;
			int currentEntryNumber = 0;
			
            for (int lastInput = ReadNextInput(); lastInput != 99999; lastInput = ReadNextInput())
            {
				currentEntryNumber++;
				currentAverage = currentAverage + (1.0/(float)currentEntryNumber)*lastInput - (1.0/(float)currentEntryNumber)*currentAverage;
				System.Console.WriteLine("New Average: " + currentAverage);
			}
        }
    }
}



Java: class recurringrainfall
{ 
	private static int GetNextInt()
	{
		while (true)
		{
			System.out.print("Enter rainfall int, 99999 to quit: ");
			String input = System.console().readLine();
        
			try
			{
				int n = Integer.parseInt(input);
				return n;
			}
			catch (Exception e)
			{
				System.out.println("Invalid input");
			}
		}
	}
	
    private static void recurringRainfall() {
		float currentAverage = 0;
		int currentEntryNumber = 0;
		
		while (true) {
			int entry = GetNextInt();
			
			if (entry == 99999)
				return;
			
			currentEntryNumber++;
			currentAverage = currentAverage + ((float)1/currentEntryNumber)*entry - ((float)1/currentEntryNumber)*currentAverage;
			
			System.out.println("New Average: " + currentAverage);
		}
    }
    
    public static void main(String args[]) { 
        recurringRainfall();
    } 
}
Task: Soloway's recurring rainfall



C#: namespace RosettaCode
{
	class CSharpRecurringRainfall
    {		
		static int ReadNextInput()
		{
			System.Console.Write("Enter rainfall int, 99999 to quit: ");
			string input = System.Console.ReadLine();
			
			if (System.Int32.TryParse(input, out int num))
			{
				return num;
			}
			else
			{
				System.Console.WriteLine("Invalid input");
				return ReadNextInput();
			}
		}
		
        static void Main()
        {
			double currentAverage = 0;
			int currentEntryNumber = 0;
			
            for (int lastInput = ReadNextInput(); lastInput != 99999; lastInput = ReadNextInput())
            {
				currentEntryNumber++;
				currentAverage = currentAverage + (1.0/(float)currentEntryNumber)*lastInput - (1.0/(float)currentEntryNumber)*currentAverage;
				System.Console.WriteLine("New Average: " + currentAverage);
			}
        }
    }
}





Python: import sys

def get_next_input():
    try:
        num = int(input("Enter rainfall int, 99999 to quit: "))
    except:
        print("Invalid input")
        return get_next_input()
    return num

current_average = 0.0
current_count = 0

while True:
    next = get_next_input()

    if next == 99999:
        sys.exit()
    else:
        current_count += 1
        current_average = current_average + (1.0/current_count)*next - (1.0/current_count)*current_average
        
        print("New average: ", current_average)
Task: Soloway's recurring rainfall





Java: class recurringrainfall
{ 
	private static int GetNextInt()
	{
		while (true)
		{
			System.out.print("Enter rainfall int, 99999 to quit: ");
			String input = System.console().readLine();
        
			try
			{
				int n = Integer.parseInt(input);
				return n;
			}
			catch (Exception e)
			{
				System.out.println("Invalid input");
			}
		}
	}
	
    private static void recurringRainfall() {
		float currentAverage = 0;
		int currentEntryNumber = 0;
		
		while (true) {
			int entry = GetNextInt();
			
			if (entry == 99999)
				return;
			
			currentEntryNumber++;
			currentAverage = currentAverage + ((float)1/currentEntryNumber)*entry - ((float)1/currentEntryNumber)*currentAverage;
			
			System.out.println("New Average: " + currentAverage);
		}
    }
    
    public static void main(String args[]) { 
        recurringRainfall();
    } 
}



Python: import sys

def get_next_input():
    try:
        num = int(input("Enter rainfall int, 99999 to quit: "))
    except:
        print("Invalid input")
        return get_next_input()
    return num

current_average = 0.0
current_count = 0

while True:
    next = get_next_input()

    if next == 99999:
        sys.exit()
    else:
        current_count += 1
        current_average = current_average + (1.0/current_count)*next - (1.0/current_count)*current_average
        
        print("New average: ", current_average)
Task: Soloway's recurring rainfall


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

int main()
{
	float currentAverage = 0;
	unsigned int currentEntryNumber = 0;
	
	for (;;)
	{
		int entry;
		
		std::cout << "Enter rainfall int, 99999 to quit: ";
		std::cin >> entry;
		
		if (!std::cin.fail())
		{
			if (entry == 99999)
			{
				std::cout << "User requested quit." << std::endl;
				break;
			}
			else
			{
				currentEntryNumber++;
				currentAverage = currentAverage + (1.0f/currentEntryNumber)*entry - (1.0f/currentEntryNumber)*currentAverage;
				
				std::cout << "New Average: " << currentAverage << std::endl;
			}
		}
		else
		{
			std::cout << "Invalid input" << std::endl;
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	}
	
	return 0;
}


C#: namespace RosettaCode
{
	class CSharpRecurringRainfall
    {		
		static int ReadNextInput()
		{
			System.Console.Write("Enter rainfall int, 99999 to quit: ");
			string input = System.Console.ReadLine();
			
			if (System.Int32.TryParse(input, out int num))
			{
				return num;
			}
			else
			{
				System.Console.WriteLine("Invalid input");
				return ReadNextInput();
			}
		}
		
        static void Main()
        {
			double currentAverage = 0;
			int currentEntryNumber = 0;
			
            for (int lastInput = ReadNextInput(); lastInput != 99999; lastInput = ReadNextInput())
            {
				currentEntryNumber++;
				currentAverage = currentAverage + (1.0/(float)currentEntryNumber)*lastInput - (1.0/(float)currentEntryNumber)*currentAverage;
				System.Console.WriteLine("New Average: " + currentAverage);
			}
        }
    }
}
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}




Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}







Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());


C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());




Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());







VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}


Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}
Task: Sum and product of an array



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}





Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array





Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}



Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array





Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}




VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array




Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}




Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array







Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i


VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}





Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}








VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());



Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());






Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}






VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array




Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}





VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Sum and product of an array



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}



Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}
Task: Sum and product of an array




Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}


Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}
Task: Sum and product of an array

C: 
int arg[] = { 1,2,3,4,5 };
int arg_length = sizeof(arg)/sizeof(arg[0]);
int *end = arg+arg_length;
int sum = 0, prod = 1;
int *p;

for (p = arg; p!=end; ++p) {
   sum += *p;
   prod *= *p;
}






PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);
Task: Sum and product of an array



C#: int sum = 0, prod = 1;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) {
  sum += value;
  prod *= value;
}




PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);
Task: Sum and product of an array


C++: #include <numeric>
#include <functional>

int arg[] = { 1, 2, 3, 4, 5 };
int sum  = std::accumulate(arg, arg+5, 0, std::plus<int>());



int prod = std::accumulate(arg, arg+5, 1, std::multiplies<int>());





PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);
Task: Sum and product of an array




Go: package main

import "fmt"

func main() {
    sum, prod := 0, 1
    for _, x := range []int{1,2,5} {
        sum += x
        prod *= x
    }
    fmt.Println(sum, prod)
}



PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);
Task: Sum and product of an array





Java: public class SumProd
{
 public static void main(final String[] args)
 {
  int sum = 0;
  int prod = 1;
  int[] arg = {1,2,3,4,5};
  for (int i : arg)
  {
   sum += i;
   prod *= i;
  }
 }
}


PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);
Task: Sum and product of an array






PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);


Python: numbers = [1, 2, 3]
total = sum(numbers)

product = 1
for i in numbers:
    product *= i
Task: Sum and product of an array






PHP: $array = array(1,2,3,4,5,6,7,8,9);
echo array_sum($array);
echo array_product($array);



VB: Sub Demo()
Dim arr
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Debug.Print "sum : " & Application.WorksheetFunction.Sum(arr)
    Debug.Print "product : " & Application.WorksheetFunction.Product(arr)
End Sub
Task: Strip whitespace from a string_Top and tail

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

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}



C#: using System;

public class TrimExample
{
    public static void Main(String[] args)
    {
        const string toTrim = " Trim me ";
        Console.WriteLine(Wrap(toTrim.TrimStart()));
        Console.WriteLine(Wrap(toTrim.TrimEnd()));
        Console.WriteLine(Wrap(toTrim.Trim()));
    }

    private static string Wrap(string s)
    {
        return "'" + s + "'";
    }
}
Task: Strip whitespace from a string_Top and tail

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

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}


C++: #include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>

int main( ) {
   std::string testphrase( "    There are unwanted blanks here!    " ) ;
   std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
   std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
   std::cout << "The test phrase is :" << testphrase << "\n" ;
   std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
   std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
   boost::trim( testphrase ) ;
   std::cout << "Trimmed on both sides :" <<  testphrase  << "\n" ;
   return 0 ;
}
Task: Strip whitespace from a string_Top and tail

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

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}




Go: package main

import (
    "fmt"
    "strings"
    "unicode"
)

var simple = `
    simple   `

func main() {
    show("original", simple)
    show("leading ws removed", strings.TrimLeftFunc(simple, unicode.IsSpace))
    show("trailing ws removed", strings.TrimRightFunc(simple, unicode.IsSpace))
    
    show("both removed", strings.TrimSpace(simple))
}

func show(label, str string) {
    fmt.Printf("%s: |%s| %v\n", label, str, []rune(str))
}
Task: Strip whitespace from a string_Top and tail

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

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}





Java: public class Trims{
    public static String ltrim(String s) {
        int i = 0;
        while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
            i++;
        }
        return s.substring(i);
    }

    public static String rtrim(String s) {
        int i = s.length() - 1;
        while (i > 0 && Character.isWhitespace(s.charAt(i))) {
            i--;
        }
        return s.substring(0, i + 1);
    }

    public static String trim(String s) {
    	return rtrim(ltrim(s));
    }

    public static void main(String[] args) {
        String s = " \t \r \n String with spaces \u2009 \t  \r  \n  ";
        System.out.printf("[%s]\n", ltrim(s));
        System.out.printf("[%s]\n", rtrim(s));
        System.out.printf("[%s]\n", trim(s));
    }
}
Task: Strip whitespace from a string_Top and tail

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

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}








VB: Public Sub test()
    
    
    
    s = " trim "
    Debug.Print """" & s & """"
    Debug.Print """" & LTrim(s) & """"
    Debug.Print """" & RTrim(s) & """"
    Debug.Print """" & WorksheetFunction.trim(s) & """"
    
End Sub
Task: Strip whitespace from a string_Top and tail


C++: #include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>

int main( ) {
   std::string testphrase( "    There are unwanted blanks here!    " ) ;
   std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
   std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
   std::cout << "The test phrase is :" << testphrase << "\n" ;
   std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
   std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
   boost::trim( testphrase ) ;
   std::cout << "Trimmed on both sides :" <<  testphrase  << "\n" ;
   return 0 ;
}


C#: using System;

public class TrimExample
{
    public static void Main(String[] args)
    {
        const string toTrim = " Trim me ";
        Console.WriteLine(Wrap(toTrim.TrimStart()));
        Console.WriteLine(Wrap(toTrim.TrimEnd()));
        Console.WriteLine(Wrap(toTrim.Trim()));
    }

    private static string Wrap(string s)
    {
        return "'" + s + "'";
    }
}