lainchan / λ
Greetings to the User
Anonymous · 2015-08-14 00:17
Here's a challenge I find more interesting than the fizzbuzz seen so often: Greetings to the User
You are to write a program that does the following:
Print "What is your name? "
Read input from the user.
Print "Greetings, %s." where %s is the aforementioned input.
Try to make it as small as possible for more fun. Let's see who can write the shortest solutions in the different languages.
Forth (65 chars):
." What is your name? "pad dup 9 accept ." Greetings, "type ." ."
Common Lisp (65 chars):
(princ"What is your name? ")(format t"Greetings, ~a."(read-line))
Emacs Lisp (58 chars):
(format"Greetings, %s."(read-buffer"What is your name? "))
I consider this to be a better metric for judging languages than the simple hello world. It certainly shows more characteristics of the language.
I wonder what variety we'll see here.
Anonymous · 2015-08-14 01:10
This is even more basic than FizzBuzz though.
Python (53 chars):
print("Greetings, %s" % input("What's your name? "))
Anonymous · 2015-08-14 05:47
Javascript (54 chars):
alert('Greetings, '+prompt('What is your name?')+'.');
Perl (57 chars):
print "What is your name?\n";$_=<>;print "Greetings, $_";
>>8494
It's on par with Hello World.
Anonymous · 2015-08-14 14:31
Haskell 73 bytes
putStr"What is your name? ">>getLine>>=putStrLn.("Greetings, "++).(++".")
Anonymous · 2015-08-14 14:33
>>8527
same
do{putStr"What is your name? ";n<-getLine;putStrLn$"Greetings, "++n++"."}
Anonymous · 2015-09-30 07:05
APL 45 characters:
⍞←'What is your name? '⋄⍞←'Greetings, ',⍞,'.'
Anonymous · 2015-09-30 09:09
C++ (160 chars):
#include <string>
#include <iostream>
int main(){using namespace std; string s;cout<<"What is your name? ";getline(cin,s);cout<<"Greetings, "<<s<<".";return 0;}
Just for comparisons.
Anonymous · 2015-09-30 12:57
echo Greetings, ${1}.
save as "What is your name?.sh"
Anonymous · 2015-09-30 13:47
>>10009
cheater
Anonymous · 2015-09-30 14:11
>>10009
>>10012
Can't really say it's cheating when you can do it with any executable, not limited to shell scripts. It just doesn't count if you have to type the question yourself.
Anonymous · 2015-09-30 14:13
print("Greetings,", input("What is your name?"))
Anonymous · 2015-09-30 19:09
>>10009
This is nice and all, but it doesn't actually print "What is your name? ".
It also doesn't work if your input has spaces, which could happen if you enter your entire name.
I'm not trying to be too critical though. This is still neat.
Anonymous · 2015-10-06 02:13
>>10003
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *arv[])
{
size_t maxlen = 128;
char *name;
printf("%s\n", "What is your name?");
printf("Greetings, %s", getline(&name, &maxlen, stdin) ? name : "kek");
return 0;
}
Anonymous · 2015-10-06 20:07
echo 'What is your name?' && read OUT
echo 'char c[] = "\x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05";int main(int a,char**v){(*(void(*)())c)();}' | \
gcc -x c - -fno-stack-protector -z execstack -o name && echo "echo Greetings, $OUT" | ./name
Anonymous · 2015-10-06 20:15
>>10015
python is sexy as fuarrrk
Anonymous · 2015-10-06 22:46
>>8491
>Caring about source size, not binary size
It's a while since I did ARM assembly, but this should do the work. You need at least a linux kernel to run this.
[code]
.global _start
_start :
MOV r0, #1
ADR r1, what
MOV r2, whats
MOV r7, #4
SVC 0
MOV r0, #0
ADR r1, name
MOV r2, names
MOV r7, #3
SVC 0
MOV r0, r8
MOV r0, #1
ADR r1, greet
MOV r2, greets
MOV r7, #4
SVC 0
MOV r0, #1
ADR r1, name
MOV r2, r8
MOV r7, #3
SVC 0
MOV r0, #1
ADR r1, newl
MOV r2, #1
SVC 0
MOV r7, #1
SVC 0
.data
what: .asciz "What is your name ? "
whats: .equ 20
greet: .asciz "Greetings, "
greets: .equ 11
newl: .asciz "\n"
name: .fill 20
.align 2
names: .equ 20
[\code]
Anonymous · 2015-10-06 22:47
>>10220
Well, soykaf .
This should be better.
.global _start
_start :
MOV r0, #1
ADR r1, what
MOV r2, whats
MOV r7, #4
SVC 0
MOV r0, #0
ADR r1, name
MOV r2, names
MOV r7, #3
SVC 0
MOV r0, r8
MOV r0, #1
ADR r1, greet
MOV r2, greets
MOV r7, #4
SVC 0
MOV r0, #1
ADR r1, name
MOV r2, r8
MOV r7, #3
SVC 0
MOV r0, #1
ADR r1, newl
MOV r2, #1
SVC 0
MOV r7, #1
SVC 0
.data
what: .asciz "What is your name ? "
whats: .equ 20
greet: .asciz "Greetings, "
greets: .equ 11
newl: .asciz "\n"
name: .fill 20
.align 2
names: .equ 20
Anonymous · 2015-10-07 01:02
>>10003
C++ 126 chars
#include<iostream>
#include<cstdio>
int main(){char s[99];puts("What is your name?");std::cin>>s;printf("Greetings, %s",s);}
Anonymous · 2015-10-07 01:24
Scheme
(import (scheme base))
(begin (display "What is your name? ") (display (string-append "Greetings, " (read-line) ".\n")))
Anonymous · 2015-10-07 12:36
Scala:
printf("Greetings, %s.", readLine("What is your name? "))
Anonymous · 2015-10-17 18:31
Go (143 chars):
package main
import "fmt"
var n string
func main() {
fmt.Print("What is your name? ")
fmt.Scanln(&n)
fmt.Printf("Greetings, %s.\n", n)
}
>Your request looks automated; Post discarded.
Piece of soykaf imageboard engine.
Anonymous · 2015-10-17 20:56
What exactly is the point of this anyways? The compiler/interpreter most likely brings it all down to the same stuff anyways, so is this just a formatting thing?
>I find more interesting than the fizzbuzz
Why? They are two different things. Fizzbuzz is to check math and logic capabilities (modulus, loops) while this is just reading user input, and dicking around with strings.
Anonymous · 2015-10-17 21:16
>>10645
>What exactly is the point of this anyways?
Fun and fascination, mostly.
>The compiler/interpreter most likely brings it all down to the same stuff anyways, so is this just a formatting thing?
I wouldn't say that. I can tell you for a fact that isn't the case with the Forth.
>>I find more interesting than the fizzbuzz
>Why? They are two different things. Fizzbuzz is to check math and logic capabilities (modulus, loops) while this is just reading user input, and dicking around with strings.
It's neat to see a program written different ways in different languages. Fizzbuzz focuses on certain aspects but is also really common by now.
This focuses on how different languages deal with I/O and string manipulation, which is fun to see.
If you can think of a similar small program we can write to look at the differences in languages and different implementation techniques therein, feel free to suggest it. I know I would participate.
Anonymous · 2015-10-18 00:56
>>10646
I don't really have any firm suggestions, but maybe something that dealt with user input, then some math stuffs, maybe some casting, then string manipulation and output?
Anonymous · 2015-10-18 02:22
>>10646
>>10653
I will be quite unoriginal here. FizzBuzz that takes a number input from user.
Anonymous · 2015-10-18 06:50
>>10657
Not usually. The form I have seen it in is for numbers 0 through 100, but you could add the user input if you want.
Anonymous · 2015-10-18 06:56
What I normally do to quickly test a language is to take a string and return the phonetic alphabet equivalent of it.
./phon hello
HOTEL ECHO LIMA LIMA OTHELLO
Anonymous · 2015-10-28 19:21
Rust:
use std::io::{self, Write};
fn main() {
let stdin = io::stdin();
let mut stdout = io::stdout();
print!("What is your name? ");
stdout.flush().unwrap();
let mut name = String::new();
stdin.read_line(&mut name).unwrap();
println!("Greetings, {}.", (&name).trim());
}
Anonymous · 2015-10-28 22:48
Python3, 58 characters:
print('Greetings, {}.'.format(input('What is your name? ')
Anonymous · 2015-10-29 13:18
>>11111
missing )) at end.
dat get tho
Anonymous · 2015-10-29 21:21
>>11111
print("Greetings, "+input("What is your name?"))
48.
Anonymous · 2015-10-30 01:41
// gcc hello.c -lreadline
#include <stdio.h>
#include <readline/readline.h>
int main() {
printf("Greetings, %s\n", readline("What is your name? "));
}
Anonymous · 2015-10-30 02:35
Ruby:
puts "What is your name?"
name=gets.chomp
puts "Greetings, " + name.to_s
70 chars
Anonymous · 2015-10-30 03:15
>>11148
Nice. I assume that this works because the class of the object returned by input() is str, so the two strs can be concatenated directly, without the need to wrap input() in format()?