You are encouraged to solve this task according to the task description, using any language you may know.
Move the cursor to column 3, row 6 and display the word "Hello", so that the letter H is in column 3 on row 6.
Contents
- 1 Ada
- 2 AutoHotkey
- 3 Axe
- 4 BASIC
- 5 Befunge
- 6 Blast
- 7 C/C++
- 8 C#
- 9 COBOL
- 10 D
- 11 Elena
- 12 Euphoria
- 13 F#
- 14 Forth
- 15 Fortran
- 16 Go
- 17 Icon and Unicon
- 18 J
- 19 Julia
- 20 Kotlin
- 21 Lasso
- 22 Liberty BASIC
- 23 Logo
- 24 Mathematica
- 25 OCaml
- 26 Pascal
- 27 Perl
- 28 Nim
- 29 Perl 6
- 30 Phix
- 31 PicoLisp
- 32 PowerShell
- 33 PureBasic
- 34 Python
- 35 Racket
- 36 REXX
- 37 Retro
- 38 Ring
- 39 Ruby
- 40 Scala
- 41 Seed7
- 42 Tcl
- 43 UNIX Shell
- 44 Whitespace
- 45 XPL0
- 46 zkl
Ada[edit]
with Ada.Text_IO;
procedure Cursor_Pos is
begin
Ada.Text_IO.Set_Line(6);
Ada.Text_IO.Set_Col(3);
Ada.Text_IO.Put("Hello");
end Cursor_Pos;
AutoHotkey[edit]
Remember that AHK is not built for the console, so we must call the WinAPI directly.
DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
DllCall("SetConsoleCursorPosition", UPtr, hConsole, UInt, (6 << 16) | 3)
WriteConsole(hConsole, "Hello")
MsgBox
WriteConsole(hConsole, text){
VarSetCapacity(out, 16)
If DllCall( "WriteConsole", UPtr, hConsole, Str, text, UInt, StrLen(text)
, UPtrP, out, uint, 0 )
return out
return 0
}
Axe[edit]
Since the rows and columns are zero-indexed, we must subtract 1 from both.
Output(2,5,"HELLO")
BASIC[edit]
Applesoft BASIC[edit]
10 VTAB 6: HTAB 3
20 PRINT "HELLO"
IS-BASIC[edit]
100 PRINT AT 6,3:"Hello"
Locomotive Basic[edit]
10 LOCATE 3,6
20 PRINT "Hello"
ZX Spectrum Basic[edit]
10 REM The top left corner is at position 0,0
20 REM So we subtract one from the coordinates
30 PRINT AT 5,2 "Hello"
BBC BASIC[edit]
PRINT TAB(2,5);"Hello"
Commodore BASIC[edit]
100 print chr$(19) :rem change to lowercase set
110 print chr$(14) :rem go to position 1,1
120 print:print:print:print
130 print tab(2) "Hello"
Befunge[edit]
Assuming a terminal with support for ANSI escape sequences.
0"olleHH3;6["39*>:#,[email protected]
Blast[edit]
# This will display a message at a specific position on the terminal screen
.begin
cursor 6,3
display "Hello!"
return
# This is the end of the script
C/C++[edit]
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x:#include <stdio.h>
int main()
{
printf("\033[6;3HHello\n");
return 0;
}
The C version of the minesweeper game uses curses. Minesweeper_game#C
On Windows, using console API:
#include <windows.h>
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {3, 6};
SetConsoleCursorPosition(hConsole, pos);
WriteConsole(hConsole, "Hello", 5, NULL, NULL);
return 0;
}
C#[edit]
static void Main(string[] args)
{
Console.SetCursorPosition(3, 6);
Console.Write("Hello");
}
COBOL[edit]
IDENTIFICATION DIVISION.
PROGRAM-ID. cursor-positioning.
PROCEDURE DIVISION.
DISPLAY "Hello" AT LINE 6, COL 3
GOBACK
.
D[edit]
ANSI escape sequences allow you to move the cursor anywhere on the screen. See more at: Bash Prompt HowTo - Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement
Position the Cursor: \033[<L>;<C>H or \033[<L>;<C>f puts the cursor at line L and column C.
import std.stdio;
void main()
{
writef("\033[6;3fHello");
}
Output:
0123456789 1 2 3 4 5 6 Hello 9 8 9
Elena[edit]
ELENA 3.4 :
public program
[
console setCursorPosition(3,6); write("Hello").
]
Euphoria[edit]
position(6,3)
puts(1,"Hello")
F#[edit]
open System
Console.SetCursorPosition(3, 6)
Console.Write("Hello")
Forth[edit]
2 5 at-xy ." Hello"
Fortran[edit]
Intel Fortran on Windows[edit]
program textposition
use kernel32
implicit none
integer(HANDLE) :: hConsole
integer(BOOL) :: q
hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
q = SetConsoleCursorPosition(hConsole, T_COORD(3, 6))
q = WriteConsole(hConsole, loc("Hello"), 5, NULL, NULL)
end program
Go[edit]
External command[edit]
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("tput", "-S")
cmd.Stdin = bytes.NewBufferString("clear\ncup 5 2")
cmd.Stdout = os.Stdout
cmd.Run()
fmt.Println("Hello")
}
ANSI escape codes[edit]
package main
import "fmt"
func main() {
fmt.Println("\033[2J\033[6;3HHello")
}
Ncurses[edit]
package main
import (
"log"
gc "code.google.com/p/goncurses"
)
func main() {
s, err := gc.Init()
if err != nil {
log.Fatal("init:", err)
}
defer gc.End()
s.Move(5, 2)
s.Println("Hello")
s.GetChar()
}
Icon and Unicon[edit]
If the OS has older termcap files, CUP is included with link ansi
procedure main()
writes(CUP(6,3), "Hello")
end
procedure CUP(i,j)
writes("\^[[",i,";",j,"H")
return
end
J[edit]
Using terminal positioning verbs of Terminal_control/Coloured_text#J
'Hello',~move 6 3
Julia[edit]
const ESC = "\u001B"
gotoANSI(x, y) = print("$ESC[$(y);$(x)H")
gotoANSI(3, 6)
println("Hello")
Kotlin[edit]
// version 1.1.2
fun main(args: Array<String>) {
print("\u001Bc") // clear screen first
println("\u001B[6;3HHello")
}
Lasso[edit]
local(esc = decode_base64('Gw=='))
stdout( #esc + '[6;3HHello')
Liberty BASIC[edit]
locate 3, 6
print "Hello"
Logo[edit]
setcursor [2 5]
type "Hello
You can also draw positioned text on the turtle graphics window.
setpos [20 50]
setxy 20 30 ; alternate way to set position
label "Hello
Mathematica[edit]
Run["tput cup 6 3"]
Print["Hello"]
OCaml[edit]
Using the library ANSITerminal:
#load "unix.cma"
#directory "+ANSITerminal"
#load "ANSITerminal.cma"
module Trm = ANSITerminal
let () =
Trm.erase Trm.Screen;
Trm.set_cursor 3 6;
Trm.print_string [] "Hello";
;;
Pascal[edit]
program cursor_pos;
uses crt;
begin
gotoxy(6,3);
write('Hello');
end.
Perl[edit]
Using the Term::Cap module:
use Term::Cap;
my $t = Term::Cap->Tgetent;
print $t->Tgoto("cm", 2, 5); # 0-based
print "Hello";
Nim[edit]
import terminal
setCursorPos(3, 6)
echo "Hello"
Perl 6[edit]
Assuming an ANSI terminal:
print "\e[6;3H";
print 'Hello';
Phix[edit]
position(6,3)
puts(1,"Hello")
PicoLisp[edit]
(call 'tput "cup" 6 3)
(prin "Hello")
PowerShell[edit]
The following will only work in the PowerShell console host. Most notably it will not work in the PowerShell ISE.
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 2,5
$Host.UI.Write('Hello')
Alternatively, in any PowerShell host that uses the Windows console, one can directly use the .NET Console
class:
[Console]::SetCursorPosition(2,5)
[Console]::Write('Hello')
PureBasic[edit]
EnableGraphicalConsole(#True)
ConsoleLocate(3,6)
Print("Hello")
Python[edit]
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x:print("\033[6;3HHello")
On Windows it needs to import and init the colorama module first.
ANSI sequences are not recognized in Windows console, here is a program using Windows API:
from ctypes import *
STD_OUTPUT_HANDLE = -11
class COORD(Structure):
pass
COORD._fields_ = [("X", c_short), ("Y", c_short)]
def print_at(r, c, s):
h = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
windll.kernel32.SetConsoleCursorPosition(h, COORD(c, r))
c = s.encode("windows-1252")
windll.kernel32.WriteConsoleA(h, c_char_p(c), len(c), None, None)
print_at(6, 3, "Hello")
Racket[edit]
#lang racket
(require (planet neil/charterm:3:0))
(with-charterm
(charterm-clear-screen)
(charterm-cursor 3 6)
(displayln "Hello World"))
REXX[edit]
The REXX language doesn't have any cursor or screen management tools, but some REXX interpreters have added the functionality via different methods.
/*REXX program demonstrates cursor position and writing of text to same.*/
call cursor 3,6 /*move the cursor to row 3, col 6*/
say 'Hello' /*write the text at that location*/
call scrwrite 30,50,'Hello.' /*another method. */
call scrwrite 40,60,'Hello.',,,14 /*another ... in yellow.*/
Retro[edit]
with console'
: hello 3 6 at-xy "Hello" puts ;
Ring[edit]
# Project : Terminal control/Cursor positioning
for n = 1 to 5
see nl
next
see " Hello"
Output:
Hello
Ruby[edit]
require 'curses'
Curses.init_screen
begin
Curses.setpos(6, 3) # column 6, row 3
Curses.addstr("Hello")
Curses.getch # Wait until user presses some key.
ensure
Curses.close_screen
end
Scala[edit]
object Main extends App {
print("\u001Bc") // clear screen first
println("\u001B[6;3HHello")
}
Seed7[edit]
The function setPos is portable and positions the cursor on the console window. SetPos is based on terminfo respectively the Windows console API.
$ include "seed7_05.s7i";
include "console.s7i";
const proc: main is func
local
var text: console is STD_NULL;
begin
console := open(CONSOLE);
setPos(console, 6, 3);
write(console, "Hello");
# Terminal windows often restore the previous
# content, when a program is terminated. Therefore
# the program waits until Return/Enter is pressed.
readln;
end func;
Tcl[edit]
exec tput cup 5 2 >/dev/tty
puts "Hello"
UNIX Shell[edit]
# The tput utility numbers from zero, so we have subtracted 1 from row and column
# number to obtain correct positioning.
tput cup 5 2
Whitespace[edit]
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x (see below):
This solution was generated from the following pseudo-Assembly.
push "Hello" ;The characters are pushed onto the stack in reverse order
push "[6;3H"
push 27 ;ESC
push 11 ;Number of characters to print
call 0 ;Calls print-string function
exit
0:
dup jumpz 1 ;Return if counter is zero
exch prtc ;Swap counter with the next character and print it
push 1 sub ;Subtract one from counter
jump 0 ;Loop back to print next character
1:
pop ret ;Pop counter and return
XPL0[edit]
include c:\cxpl\codes; \intrinsic 'code' declarations
[Cursor(2, 5); \3rd column, 6th row
Text(0, "Hello"); \upper-left corner is coordinate 0, 0
]
zkl[edit]
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x:
print("\e[6;3H" "Hello");
- Programming Tasks
- Terminal control
- Ada
- AutoHotkey
- Axe
- BASIC
- Applesoft BASIC
- IS-BASIC
- Locomotive Basic
- ZX Spectrum Basic
- BBC BASIC
- Commodore BASIC
- Befunge
- Blast
- C
- C++
- C sharp
- COBOL
- D
- Elena
- Euphoria
- F Sharp
- Forth
- Fortran
- Go
- Curses
- Icon
- Unicon
- J
- Julia
- Kotlin
- Lasso
- Liberty BASIC
- Logo
- Mathematica
- OCaml
- Pascal
- Perl
- Nim
- Perl 6
- Phix
- PicoLisp
- PowerShell
- PureBasic
- Python
- Racket
- REXX
- Retro
- Ring
- Ruby
- Scala
- Seed7
- Tcl
- UNIX Shell
- Whitespace
- XPL0
- Zkl
- ACL2/Omit
- GUISS/Omit
- Maxima/Omit
- PARI/GP/Omit
- Scratch/Omit