Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Produce a language-to-language conversion: from Tcl to C#, same semantics.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
using System; using System.IO; using System.Linq; public class Rosetta { public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2); static void RemoveLines(string filename, int start, int count = 1) => File.WriteAllLines(filename, File.ReadAllLines(filename) .Where((line, index) => index < start - 1 || index >= start + count - 1)); }
Generate an equivalent C++ version of this Tcl code.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
#include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <list> void deleteLines( const std::string & , int , int ) ; int main( int argc, char * argv[ ] ) { if ( argc != 4 ) { std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ; return 1 ; } std::string filename( argv[ 1 ] ) ; int startfrom = atoi( argv[ 2 ] ) ; int howmany = atoi( argv[ 3 ] ) ; deleteLines ( filename , startfrom , howmany ) ; return 0 ; } void deleteLines( const std::string & filename , int start , int skip ) { std::ifstream infile( filename.c_str( ) , std::ios::in ) ; if ( infile.is_open( ) ) { std::string line ; std::list<std::string> filelines ; while ( infile ) { getline( infile , line ) ; filelines.push_back( line ) ; } infile.close( ) ; if ( start > filelines.size( ) ) { std::cerr << "Error! Starting to delete lines past the end of the file!\n" ; return ; } if ( ( start + skip ) > filelines.size( ) ) { std::cerr << "Error! Trying to delete lines past the end of the file!\n" ; return ; } std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ; for ( int i = 1 ; i < start ; i++ ) deletebegin++ ; deleteend = deletebegin ; for( int i = 0 ; i < skip ; i++ ) deleteend++ ; filelines.erase( deletebegin , deleteend ) ; std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ; if ( outfile.is_open( ) ) { for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ; sli != filelines.end( ) ; sli++ ) outfile << *sli << "\n" ; } outfile.close( ) ; } else { std::cerr << "Error! Could not find file " << filename << " !\n" ; return ; } }
Ensure the translated C++ code behaves exactly like the original Tcl snippet.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
#include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <list> void deleteLines( const std::string & , int , int ) ; int main( int argc, char * argv[ ] ) { if ( argc != 4 ) { std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ; return 1 ; } std::string filename( argv[ 1 ] ) ; int startfrom = atoi( argv[ 2 ] ) ; int howmany = atoi( argv[ 3 ] ) ; deleteLines ( filename , startfrom , howmany ) ; return 0 ; } void deleteLines( const std::string & filename , int start , int skip ) { std::ifstream infile( filename.c_str( ) , std::ios::in ) ; if ( infile.is_open( ) ) { std::string line ; std::list<std::string> filelines ; while ( infile ) { getline( infile , line ) ; filelines.push_back( line ) ; } infile.close( ) ; if ( start > filelines.size( ) ) { std::cerr << "Error! Starting to delete lines past the end of the file!\n" ; return ; } if ( ( start + skip ) > filelines.size( ) ) { std::cerr << "Error! Trying to delete lines past the end of the file!\n" ; return ; } std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ; for ( int i = 1 ; i < start ; i++ ) deletebegin++ ; deleteend = deletebegin ; for( int i = 0 ; i < skip ; i++ ) deleteend++ ; filelines.erase( deletebegin , deleteend ) ; std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ; if ( outfile.is_open( ) ) { for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ; sli != filelines.end( ) ; sli++ ) outfile << *sli << "\n" ; } outfile.close( ) ; } else { std::cerr << "Error! Could not find file " << filename << " !\n" ; return ; } }
Convert the following code from Tcl to Java, ensuring the logic remains intact.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class RemoveLines { public static void main(String[] args) { String filename="foobar.txt"; int startline=1; int numlines=2; RemoveLines now=new RemoveLines(); now.delete(filename,startline,numlines); } void delete(String filename, int startline, int numlines) { try { BufferedReader br=new BufferedReader(new FileReader(filename)); StringBuffer sb=new StringBuffer(""); int linenumber=1; String line; while((line=br.readLine())!=null) { if(linenumber<startline||linenumber>=startline+numlines) sb.append(line+"\n"); linenumber++; } if(startline+numlines>linenumber) System.out.println("End of file reached."); br.close(); FileWriter fw=new FileWriter(new File(filename)); fw.write(sb.toString()); fw.close(); } catch (Exception e) { System.out.println("Something went horribly wrong: "+e.getMessage()); } } }
Please provide an equivalent version of this Tcl code in Python.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
import fileinput, sys fname, start, count = sys.argv[1:4] start, count = int(start), int(count) for line in fileinput.input(fname, inplace=1, backup='.orig'): if start <= fileinput.lineno() < start + count: pass else: print line.rstrip("\n") fileinput.close()
Please provide an equivalent version of this Tcl code in Python.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
import fileinput, sys fname, start, count = sys.argv[1:4] start, count = int(start), int(count) for line in fileinput.input(fname, inplace=1, backup='.orig'): if start <= fileinput.lineno() < start + count: pass else: print line.rstrip("\n") fileinput.close()
Translate the given Tcl code snippet into VB without altering its behavior.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
Option Explicit Sub Main() RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5 End Sub Private Sub RemoveLines(StrFile As String, StartLine As Long, NumberOfLines As Long) Dim Nb As Integer, s As String, count As Long, out As String Nb = FreeFile Open StrFile For Input As #Nb While Not EOF(Nb) count = count + 1 Line Input #Nb, s If count < StartLine Or count >= StartLine + NumberOfLines Then out = out & s & vbCrLf End If Wend Close #Nb If StartLine >= count Then MsgBox "The file contains only " & count & " lines" ElseIf StartLine + NumberOfLines > count Then MsgBox "You only can remove " & count - StartLine & " lines" Else Nb = FreeFile Open StrFile For Output As #Nb Print #Nb, out Close #Nb End If End Sub
Transform the following Tcl implementation into VB, maintaining the same output and logic.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
Option Explicit Sub Main() RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5 End Sub Private Sub RemoveLines(StrFile As String, StartLine As Long, NumberOfLines As Long) Dim Nb As Integer, s As String, count As Long, out As String Nb = FreeFile Open StrFile For Input As #Nb While Not EOF(Nb) count = count + 1 Line Input #Nb, s If count < StartLine Or count >= StartLine + NumberOfLines Then out = out & s & vbCrLf End If Wend Close #Nb If StartLine >= count Then MsgBox "The file contains only " & count & " lines" ElseIf StartLine + NumberOfLines > count Then MsgBox "You only can remove " & count - StartLine & " lines" Else Nb = FreeFile Open StrFile For Output As #Nb Print #Nb, out Close #Nb End If End Sub
Convert this Tcl snippet to Go and keep its semantics consistent.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
package main import ( "bytes" "errors" "fmt" "io/ioutil" "os" ) func main() { if err := removeLines("foobar.txt", 1, 2); err != nil { fmt.Println(err) } } func removeLines(fn string, start, n int) (err error) { if start < 1 { return errors.New("invalid request. line numbers start at 1.") } if n < 0 { return errors.New("invalid request. negative number to remove.") } var f *os.File if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil { return } defer func() { if cErr := f.Close(); err == nil { err = cErr } }() var b []byte if b, err = ioutil.ReadAll(f); err != nil { return } cut, ok := skip(b, start-1) if !ok { return fmt.Errorf("less than %d lines", start) } if n == 0 { return nil } tail, ok := skip(cut, n) if !ok { return fmt.Errorf("less than %d lines after line %d", n, start) } t := int64(len(b) - len(cut)) if err = f.Truncate(t); err != nil { return } if len(tail) > 0 { _, err = f.WriteAt(tail, t) } return } func skip(b []byte, n int) ([]byte, bool) { for ; n > 0; n-- { if len(b) == 0 { return nil, false } x := bytes.IndexByte(b, '\n') if x < 0 { x = len(b) } else { x++ } b = b[x:] } return b, true }
Rewrite the snippet below in Go so it works the same as the original Tcl code.
proc removeLines {fileName startLine count} { set from [expr {$startLine - 1}] set to [expr {$startLine + $count - 2}] set f [open $fileName] set lines [split [read $f] "\n"] close $f set f [open $fileName w] puts -nonewline $f [join [lreplace $lines $from $to] "\n"] close $f }
package main import ( "bytes" "errors" "fmt" "io/ioutil" "os" ) func main() { if err := removeLines("foobar.txt", 1, 2); err != nil { fmt.Println(err) } } func removeLines(fn string, start, n int) (err error) { if start < 1 { return errors.New("invalid request. line numbers start at 1.") } if n < 0 { return errors.New("invalid request. negative number to remove.") } var f *os.File if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil { return } defer func() { if cErr := f.Close(); err == nil { err = cErr } }() var b []byte if b, err = ioutil.ReadAll(f); err != nil { return } cut, ok := skip(b, start-1) if !ok { return fmt.Errorf("less than %d lines", start) } if n == 0 { return nil } tail, ok := skip(cut, n) if !ok { return fmt.Errorf("less than %d lines after line %d", n, start) } t := int64(len(b) - len(cut)) if err = f.Truncate(t); err != nil { return } if len(tail) > 0 { _, err = f.WriteAt(tail, t) } return } func skip(b []byte, n int) ([]byte, bool) { for ; n > 0; n-- { if len(b) == 0 { return nil, false } x := bytes.IndexByte(b, '\n') if x < 0 { x = len(b) } else { x++ } b = b[x:] } return b, true }
Produce a functionally identical Rust code for the snippet given in C.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ERROR(fmt, arg) return fprintf(stderr, fmt "\n", arg) int main(int argc, char **argv) { FILE *fp; char *buf; size_t sz; int start, count, lines = 1; int dest = 0, src = 0, pos = -1; if (argc != 4) ERROR("Usage: %s <file> <start> <count>", argv[0]); if ((count = atoi(argv[3])) < 1) return 0; if ((start = atoi(argv[2])) < 1) ERROR("Error: <start> (%d) must be positive", start); if ((fp = fopen(argv[1], "r")) == NULL) ERROR("No such file: %s", argv[1]); fseek(fp, 0, SEEK_END); sz = ftell(fp); buf = malloc(sz + 1); rewind(fp); while ((buf[++pos] = fgetc(fp)) != EOF) { if (buf[pos] == '\n') { ++lines; if (lines == start) dest = pos + 1; if (lines == start + count) src = pos + 1; } } if (start + count > lines) { free(buf); fclose(fp); ERROR("Error: invalid parameters for file with %d lines", --lines); } memmove(buf + dest, buf + src, pos - src); freopen(argv[1], "w", fp); fwrite(buf, pos - src + dest, 1, fp); free(buf); fclose(fp); return 0; }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Generate a Rust translation of this C snippet without changing its computational steps.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ERROR(fmt, arg) return fprintf(stderr, fmt "\n", arg) int main(int argc, char **argv) { FILE *fp; char *buf; size_t sz; int start, count, lines = 1; int dest = 0, src = 0, pos = -1; if (argc != 4) ERROR("Usage: %s <file> <start> <count>", argv[0]); if ((count = atoi(argv[3])) < 1) return 0; if ((start = atoi(argv[2])) < 1) ERROR("Error: <start> (%d) must be positive", start); if ((fp = fopen(argv[1], "r")) == NULL) ERROR("No such file: %s", argv[1]); fseek(fp, 0, SEEK_END); sz = ftell(fp); buf = malloc(sz + 1); rewind(fp); while ((buf[++pos] = fgetc(fp)) != EOF) { if (buf[pos] == '\n') { ++lines; if (lines == start) dest = pos + 1; if (lines == start + count) src = pos + 1; } } if (start + count > lines) { free(buf); fclose(fp); ERROR("Error: invalid parameters for file with %d lines", --lines); } memmove(buf + dest, buf + src, pos - src); freopen(argv[1], "w", fp); fwrite(buf, pos - src + dest, 1, fp); free(buf); fclose(fp); return 0; }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Ensure the translated Rust code behaves exactly like the original C++ snippet.
#include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <list> void deleteLines( const std::string & , int , int ) ; int main( int argc, char * argv[ ] ) { if ( argc != 4 ) { std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ; return 1 ; } std::string filename( argv[ 1 ] ) ; int startfrom = atoi( argv[ 2 ] ) ; int howmany = atoi( argv[ 3 ] ) ; deleteLines ( filename , startfrom , howmany ) ; return 0 ; } void deleteLines( const std::string & filename , int start , int skip ) { std::ifstream infile( filename.c_str( ) , std::ios::in ) ; if ( infile.is_open( ) ) { std::string line ; std::list<std::string> filelines ; while ( infile ) { getline( infile , line ) ; filelines.push_back( line ) ; } infile.close( ) ; if ( start > filelines.size( ) ) { std::cerr << "Error! Starting to delete lines past the end of the file!\n" ; return ; } if ( ( start + skip ) > filelines.size( ) ) { std::cerr << "Error! Trying to delete lines past the end of the file!\n" ; return ; } std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ; for ( int i = 1 ; i < start ; i++ ) deletebegin++ ; deleteend = deletebegin ; for( int i = 0 ; i < skip ; i++ ) deleteend++ ; filelines.erase( deletebegin , deleteend ) ; std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ; if ( outfile.is_open( ) ) { for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ; sli != filelines.end( ) ; sli++ ) outfile << *sli << "\n" ; } outfile.close( ) ; } else { std::cerr << "Error! Could not find file " << filename << " !\n" ; return ; } }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Transform the following C# implementation into Rust, maintaining the same output and logic.
using System; using System.IO; using System.Linq; public class Rosetta { public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2); static void RemoveLines(string filename, int start, int count = 1) => File.WriteAllLines(filename, File.ReadAllLines(filename) .Where((line, index) => index < start - 1 || index >= start + count - 1)); }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Preserve the algorithm and functionality while converting the code from C# to Rust.
using System; using System.IO; using System.Linq; public class Rosetta { public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2); static void RemoveLines(string filename, int start, int count = 1) => File.WriteAllLines(filename, File.ReadAllLines(filename) .Where((line, index) => index < start - 1 || index >= start + count - 1)); }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Preserve the algorithm and functionality while converting the code from Java to Rust.
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class RemoveLines { public static void main(String[] args) { String filename="foobar.txt"; int startline=1; int numlines=2; RemoveLines now=new RemoveLines(); now.delete(filename,startline,numlines); } void delete(String filename, int startline, int numlines) { try { BufferedReader br=new BufferedReader(new FileReader(filename)); StringBuffer sb=new StringBuffer(""); int linenumber=1; String line; while((line=br.readLine())!=null) { if(linenumber<startline||linenumber>=startline+numlines) sb.append(line+"\n"); linenumber++; } if(startline+numlines>linenumber) System.out.println("End of file reached."); br.close(); FileWriter fw=new FileWriter(new File(filename)); fw.write(sb.toString()); fw.close(); } catch (Exception e) { System.out.println("Something went horribly wrong: "+e.getMessage()); } } }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Transform the following Go implementation into Rust, maintaining the same output and logic.
package main import ( "bytes" "errors" "fmt" "io/ioutil" "os" ) func main() { if err := removeLines("foobar.txt", 1, 2); err != nil { fmt.Println(err) } } func removeLines(fn string, start, n int) (err error) { if start < 1 { return errors.New("invalid request. line numbers start at 1.") } if n < 0 { return errors.New("invalid request. negative number to remove.") } var f *os.File if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil { return } defer func() { if cErr := f.Close(); err == nil { err = cErr } }() var b []byte if b, err = ioutil.ReadAll(f); err != nil { return } cut, ok := skip(b, start-1) if !ok { return fmt.Errorf("less than %d lines", start) } if n == 0 { return nil } tail, ok := skip(cut, n) if !ok { return fmt.Errorf("less than %d lines after line %d", n, start) } t := int64(len(b) - len(cut)) if err = f.Truncate(t); err != nil { return } if len(tail) > 0 { _, err = f.WriteAt(tail, t) } return } func skip(b []byte, n int) ([]byte, bool) { for ; n > 0; n-- { if len(b) == 0 { return nil, false } x := bytes.IndexByte(b, '\n') if x < 0 { x = len(b) } else { x++ } b = b[x:] } return b, true }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Convert the following code from Go to Rust, ensuring the logic remains intact.
package main import ( "bytes" "errors" "fmt" "io/ioutil" "os" ) func main() { if err := removeLines("foobar.txt", 1, 2); err != nil { fmt.Println(err) } } func removeLines(fn string, start, n int) (err error) { if start < 1 { return errors.New("invalid request. line numbers start at 1.") } if n < 0 { return errors.New("invalid request. negative number to remove.") } var f *os.File if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil { return } defer func() { if cErr := f.Close(); err == nil { err = cErr } }() var b []byte if b, err = ioutil.ReadAll(f); err != nil { return } cut, ok := skip(b, start-1) if !ok { return fmt.Errorf("less than %d lines", start) } if n == 0 { return nil } tail, ok := skip(cut, n) if !ok { return fmt.Errorf("less than %d lines after line %d", n, start) } t := int64(len(b) - len(cut)) if err = f.Truncate(t); err != nil { return } if len(tail) > 0 { _, err = f.WriteAt(tail, t) } return } func skip(b []byte, n int) ([]byte, bool) { for ; n > 0; n-- { if len(b) == 0 { return nil, false } x := bytes.IndexByte(b, '\n') if x < 0 { x = len(b) } else { x++ } b = b[x:] } return b, true }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Write a version of this Rust function in Python with identical behavior.
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
import fileinput, sys fname, start, count = sys.argv[1:4] start, count = int(start), int(count) for line in fileinput.input(fname, inplace=1, backup='.orig'): if start <= fileinput.lineno() < start + count: pass else: print line.rstrip("\n") fileinput.close()
Convert the following code from Rust to Python, ensuring the logic remains intact.
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
import fileinput, sys fname, start, count = sys.argv[1:4] start, count = int(start), int(count) for line in fileinput.input(fname, inplace=1, backup='.orig'): if start <= fileinput.lineno() < start + count: pass else: print line.rstrip("\n") fileinput.close()
Convert this Rust snippet to VB and keep its semantics consistent.
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Option Explicit Sub Main() RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5 End Sub Private Sub RemoveLines(StrFile As String, StartLine As Long, NumberOfLines As Long) Dim Nb As Integer, s As String, count As Long, out As String Nb = FreeFile Open StrFile For Input As #Nb While Not EOF(Nb) count = count + 1 Line Input #Nb, s If count < StartLine Or count >= StartLine + NumberOfLines Then out = out & s & vbCrLf End If Wend Close #Nb If StartLine >= count Then MsgBox "The file contains only " & count & " lines" ElseIf StartLine + NumberOfLines > count Then MsgBox "You only can remove " & count - StartLine & " lines" Else Nb = FreeFile Open StrFile For Output As #Nb Print #Nb, out Close #Nb End If End Sub
Produce a functionally identical Rust code for the snippet given in Java.
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class RemoveLines { public static void main(String[] args) { String filename="foobar.txt"; int startline=1; int numlines=2; RemoveLines now=new RemoveLines(); now.delete(filename,startline,numlines); } void delete(String filename, int startline, int numlines) { try { BufferedReader br=new BufferedReader(new FileReader(filename)); StringBuffer sb=new StringBuffer(""); int linenumber=1; String line; while((line=br.readLine())!=null) { if(linenumber<startline||linenumber>=startline+numlines) sb.append(line+"\n"); linenumber++; } if(startline+numlines>linenumber) System.out.println("End of file reached."); br.close(); FileWriter fw=new FileWriter(new File(filename)); fw.write(sb.toString()); fw.close(); } catch (Exception e) { System.out.println("Something went horribly wrong: "+e.getMessage()); } } }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Produce a functionally identical VB code for the snippet given in Rust.
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Option Explicit Sub Main() RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5 RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5 End Sub Private Sub RemoveLines(StrFile As String, StartLine As Long, NumberOfLines As Long) Dim Nb As Integer, s As String, count As Long, out As String Nb = FreeFile Open StrFile For Input As #Nb While Not EOF(Nb) count = count + 1 Line Input #Nb, s If count < StartLine Or count >= StartLine + NumberOfLines Then out = out & s & vbCrLf End If Wend Close #Nb If StartLine >= count Then MsgBox "The file contains only " & count & " lines" ElseIf StartLine + NumberOfLines > count Then MsgBox "You only can remove " & count - StartLine & " lines" Else Nb = FreeFile Open StrFile For Output As #Nb Print #Nb, out Close #Nb End If End Sub
Can you help me rewrite this code in Rust instead of C++, keeping it the same logically?
#include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <list> void deleteLines( const std::string & , int , int ) ; int main( int argc, char * argv[ ] ) { if ( argc != 4 ) { std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ; return 1 ; } std::string filename( argv[ 1 ] ) ; int startfrom = atoi( argv[ 2 ] ) ; int howmany = atoi( argv[ 3 ] ) ; deleteLines ( filename , startfrom , howmany ) ; return 0 ; } void deleteLines( const std::string & filename , int start , int skip ) { std::ifstream infile( filename.c_str( ) , std::ios::in ) ; if ( infile.is_open( ) ) { std::string line ; std::list<std::string> filelines ; while ( infile ) { getline( infile , line ) ; filelines.push_back( line ) ; } infile.close( ) ; if ( start > filelines.size( ) ) { std::cerr << "Error! Starting to delete lines past the end of the file!\n" ; return ; } if ( ( start + skip ) > filelines.size( ) ) { std::cerr << "Error! Trying to delete lines past the end of the file!\n" ; return ; } std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ; for ( int i = 1 ; i < start ; i++ ) deletebegin++ ; deleteend = deletebegin ; for( int i = 0 ; i < skip ; i++ ) deleteend++ ; filelines.erase( deletebegin , deleteend ) ; std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ; if ( outfile.is_open( ) ) { for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ; sli != filelines.end( ) ; sli++ ) outfile << *sli << "\n" ; } outfile.close( ) ; } else { std::cerr << "Error! Could not find file " << filename << " !\n" ; return ; } }
extern crate rustc_serialize; extern crate docopt; use docopt::Docopt; use std::io::{BufReader,BufRead}; use std::fs::File; const USAGE: &'static str = " Usage: rosetta <start> <count> <file> "; #[derive(Debug, RustcDecodable)] struct Args { arg_start: usize, arg_count: usize, arg_file: String, } fn main() { let args: Args = Docopt::new(USAGE) .and_then(|d| d.decode()) .unwrap_or_else(|e| e.exit()); let file = BufReader::new(File::open(args.arg_file).unwrap()); for (i, line) in file.lines().enumerate() { let cur = i + 1; if cur < args.arg_start || cur >= (args.arg_start + args.arg_count) { println!("{}", line.unwrap()); } } }
Change the following Ada code into C# without altering its purpose.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Write a version of this Ada function in C with identical behavior.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Produce a functionally identical C++ code for the snippet given in Ada.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Change the programming language of this snippet from Ada to Go without modifying what it does.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Can you help me rewrite this code in Java instead of Ada, keeping it the same logically?
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Keep all operations the same but rewrite the snippet in Python.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Produce a language-to-language conversion: from Ada to VB, same semantics.
with Ada.Characters.Handling, Ada.Text_IO; use Ada.Characters.Handling, Ada.Text_IO; procedure Upper_Case_String is S : constant String := "alphaBETA"; begin Put_Line (To_Upper (S)); Put_Line (To_Lower (S)); end Upper_Case_String;
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Convert this Arturo snippet to C and keep its semantics consistent.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Rewrite this program in C# while keeping its functionality equivalent to the Arturo version.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Preserve the algorithm and functionality while converting the code from Arturo to C++.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Produce a language-to-language conversion: from Arturo to Java, same semantics.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Change the following Arturo code into Python without altering its purpose.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Port the following code from Arturo to VB with equivalent syntax and logic.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Produce a language-to-language conversion: from Arturo to Go, same semantics.
str: "alphaBETA" print ["uppercase  :" upper str] print ["lowercase  :" lower str] print ["capitalize :" capitalize str]
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Port the provided AutoHotKey code into C while preserving the original functionality.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Translate the given AutoHotKey code snippet into C# without altering its behavior.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Write the same code in C++ as shown below in AutoHotKey.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Transform the following AutoHotKey implementation into Java, maintaining the same output and logic.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Translate the given AutoHotKey code snippet into Python without altering its behavior.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Translate the given AutoHotKey code snippet into VB without altering its behavior.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Produce a language-to-language conversion: from AutoHotKey to Go, same semantics.
a := "alphaBETA" StringLower, b, a  StringUpper, c, a  StringUpper, d, a, T 
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Write the same algorithm in C as shown in this AWK implementation.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Write the same code in C# as shown below in AWK.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Can you help me rewrite this code in C++ instead of AWK, keeping it the same logically?
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Rewrite this program in Java while keeping its functionality equivalent to the AWK version.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Convert the following code from AWK to Python, ensuring the logic remains intact.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Rewrite this program in VB while keeping its functionality equivalent to the AWK version.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Port the following code from AWK to Go with equivalent syntax and logic.
BEGIN { a = "alphaBETA"; print toupper(a), tolower(a) }
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Ensure the translated C code behaves exactly like the original BBC_Basic snippet.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Convert the following code from BBC_Basic to C#, ensuring the logic remains intact.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Write a version of this BBC_Basic function in C++ with identical behavior.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Produce a language-to-language conversion: from BBC_Basic to Java, same semantics.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Generate an equivalent Python version of this BBC_Basic code.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Ensure the translated VB code behaves exactly like the original BBC_Basic snippet.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Translate this program into Go but keep the logic exactly as in BBC_Basic.
INSTALL @lib$+"STRINGLIB" original$ = "alphaBETA" PRINT "Original: " original$ PRINT "Lower case: " FN_lower(original$) PRINT "Upper case: " FN_upper(original$) PRINT "Title case: " FN_title(original$)
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Ensure the translated C code behaves exactly like the original Common_Lisp snippet.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Please provide an equivalent version of this Common_Lisp code in C#.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Translate this program into C++ but keep the logic exactly as in Common_Lisp.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Convert this Common_Lisp snippet to Java and keep its semantics consistent.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Generate an equivalent Python version of this Common_Lisp code.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Please provide an equivalent version of this Common_Lisp code in VB.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Translate the given Common_Lisp code snippet into Go without altering its behavior.
(def string "alphaBETA") (println (.toUpperCase string)) (println (.toLowerCase string))
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Produce a functionally identical C code for the snippet given in D.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Rewrite this program in C++ while keeping its functionality equivalent to the D version.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Keep all operations the same but rewrite the snippet in Java.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Generate a Python translation of this D snippet without changing its computational steps.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Produce a language-to-language conversion: from D to VB, same semantics.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Write the same algorithm in Go as shown in this D implementation.
void main() { import std.stdio, std.string; immutable s = "alphaBETA"; s.toUpper.writeln; s.toLower.writeln; }
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Can you help me rewrite this code in C instead of Delphi, keeping it the same logically?
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Ensure the translated C# code behaves exactly like the original Delphi snippet.
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Keep all operations the same but rewrite the snippet in C++.
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Please provide an equivalent version of this Delphi code in Java.
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Can you help me rewrite this code in Python instead of Delphi, keeping it the same logically?
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Translate the given Delphi code snippet into VB without altering its behavior.
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Rewrite this program in Go while keeping its functionality equivalent to the Delphi version.
PrintLn(UpperCase('alphaBETA')); PrintLn(LowerCase('alphaBETA'));
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Produce a functionally identical C code for the snippet given in Elixir.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Produce a functionally identical C# code for the snippet given in Elixir.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Translate the given Elixir code snippet into C++ without altering its behavior.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Transform the following Elixir implementation into Java, maintaining the same output and logic.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Convert this Elixir snippet to Python and keep its semantics consistent.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Change the following Elixir code into VB without altering its purpose.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Preserve the algorithm and functionality while converting the code from Elixir to Go.
String.downcase("alphaBETA") String.upcase("alphaBETA") String.capitalize("alphaBETA")
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Rewrite this program in C while keeping its functionality equivalent to the Erlang version.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Convert this Erlang snippet to C# and keep its semantics consistent.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Convert this Erlang snippet to C++ and keep its semantics consistent.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Write a version of this Erlang function in Python with identical behavior.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Preserve the algorithm and functionality while converting the code from Erlang to VB.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Port the provided Erlang code into Go while preserving the original functionality.
string:to_upper("alphaBETA"). string:to_lower("alphaBETA").
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }
Translate the given F# code snippet into C without altering its behavior.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
#include <ctype.h> #include <stdio.h> void str_toupper(char *s) { while(*s) { *s=toupper(*s); s++; } } void str_tolower(char *s) { while(*s) { *s=tolower(*s); s++; } } int main(int argc, char *argv[]) { char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0; }
Rewrite the snippet below in C# so it works the same as the original F# code.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
using System; namespace StringCase { class Program { public static void Main() { String input = scope .("alphaBETA"); input.ToUpper(); Console.WriteLine(input); input.ToLower(); Console.WriteLine(input); } } }
Port the following code from F# to C++ with equivalent syntax and logic.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
#include <algorithm> #include <string> #include <cctype> void str_toupper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper); } void str_tolower(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower); }
Maintain the same structure and functionality when rewriting this code in Java.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println("äàâáçñßæεбế".toUpperCase()); System.out.println("ÄÀÂÁÇÑSSÆΕБẾ".toLowerCase());
Produce a language-to-language conversion: from F# to Python, same semantics.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() print "fOo bAR".capitalize() print "fOo bAR".title() import string print string.capwords("fOo bAR")
Please provide an equivalent version of this F# code in VB.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
Sub Main() Const TESTSTRING As String = "alphaBETA" Debug.Print "initial = " _ & TESTSTRING Debug.Print "uppercase = " _ & UCase(TESTSTRING) Debug.Print "lowercase = " _ & LCase(TESTSTRING) Debug.Print "first letter capitalized = " _ & StrConv(TESTSTRING, vbProperCase) Debug.Print "length (in characters) = " _ & CStr(Len(TESTSTRING)) Debug.Print "length (in bytes) = " _ & CStr(LenB(TESTSTRING)) Debug.Print "reversed = " _ & StrReverse(TESTSTRING) Debug.Print "first position of letter A (case-sensitive) = " _ & InStr(1, TESTSTRING, "A", vbBinaryCompare) Debug.Print "first position of letter A (case-insensitive) = " _ & InStr(1, TESTSTRING, "A", vbTextCompare) Debug.Print "concatenated with & TESTSTRING & "123" End Sub
Please provide an equivalent version of this F# code in Go.
let s = "alphaBETA" let upper = s.ToUpper() let lower = s.ToLower()
package main import ( "fmt" "strings" "unicode" "unicode/utf8" ) func main() { show("alphaBETA") show("alpha BETA") show("DŽLjnj") show("o'hare O'HARE o’hare don't") } func show(s string) { fmt.Println("\nstring: ", s, " len:", utf8.RuneCountInString(s), "runes") fmt.Println("All upper case: ", strings.ToUpper(s)) fmt.Println("All lower case: ", strings.ToLower(s)) fmt.Println("All title case: ", strings.ToTitle(s)) fmt.Println("Title words: ", strings.Title(s)) fmt.Println("Swapping case: ", strings.Map(unicode.SimpleFold, s)) }