[ cyb / tech / λ / layer ] [ zzz / drg / lit / diy / art ] [ w / rpg / r ] [ q ] [ / ] [ popular / ???? / rules / radio / $$ / news ] [ volafile / uboa / sushi / LainTV / lewd ]

λ - programming

/lam/bda /lam/bda duck
Name
Email
Subject
Comment
File
Password (For file deletion.)

BUY LAINCHAN STICKERS HERE

STREAM » LainTV « STREAM

[Return][Go to bottom]

File: 1439511436822.png (23.45 KB, 1084x180, greetings-to-the-user.png) ImgOps iqdb

 No.8491

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.
>>

 No.8494

This is even more basic than FizzBuzz though.

Python (53 chars):
print("Greetings, %s" % input("What's your name? "))

>>

 No.8513

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.

>>

 No.8527

Haskell 73 bytes
putStr"What is your name? ">>getLine>>=putStrLn.("Greetings, "++).(++".")

>>

 No.8528

>>8527
same
do{putStr"What is your name? ";n<-getLine;putStrLn$"Greetings, "++n++"."}

>>

 No.9996

APL 45 characters:
⍞←'What is your name? '⋄⍞←'Greetings, ',⍞,'.'

>>

 No.10003

File: 1443604195353.png (262.88 KB, 800x720, strongest_programmer.png) ImgOps iqdb

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.

>>

 No.10009

echo Greetings, ${1}.

save as "What is your name?.sh"

>>

 No.10012

>>10009
cheater

>>

 No.10014

>>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.

>>

 No.10015


print("Greetings,", input("What is your name?"))

>>

 No.10030

>>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.

>>

 No.10185

File: 1444097584956.jpg (230.14 KB, 1280x720, 8K9r7vb.jpg) ImgOps Exif iqdb

>>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;
}

>>

 No.10216


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

>>

 No.10217

>>10015
python is sexy as fuarrrk

>>

 No.10220

>>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]

>>

 No.10221

>>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

>>

 No.10227

>>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);}

>>

 No.10231

Scheme

(import (scheme base))
(begin (display "What is your name? ") (display (string-append "Greetings, " (read-line) ".\n")))

>>

 No.10244

Scala:


printf("Greetings, %s.", readLine("What is your name? "))

>>

 No.10638

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.

>>

 No.10645

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.

>>

 No.10646

>>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.

>>

 No.10653

>>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?

>>

 No.10657

>>10646
>>10653
I will be quite unoriginal here. FizzBuzz that takes a number input from user.

>>

 No.10660

>>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.

>>

 No.10661

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

>>

 No.11104

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());
}

>>

 No.11111

Python3, 58 characters:
print('Greetings, {}.'.format(input('What is your name? ')

>>

 No.11132

File: 1446124709950.jpg (75.12 KB, 454x600, 1444193826673.jpg) ImgOps Exif iqdb

>>11111
missing )) at end.
dat get tho

>>

 No.11148

>>11111

print("Greetings, "+input("What is your name?"))


48.

>>

 No.11154


// gcc hello.c -lreadline
#include <stdio.h>
#include <readline/readline.h>

int main() {
printf("Greetings, %s\n", readline("What is your name? "));
}

>>

 No.11156

Ruby:
puts "What is your name?"
name=gets.chomp
puts "Greetings, " + name.to_s

70 chars

>>

 No.11158

>>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()?



Delete Post [ ]
[ cyb / tech / λ / layer ] [ zzz / drg / lit / diy / art ] [ w / rpg / r ] [ q ] [ / ] [ popular / ???? / rules / radio / $$ / news ] [ volafile / uboa / sushi / LainTV / lewd ]