"Hello Ubuntu" in every programming language
List made by pp., posted by LaRoza
================================================== ================================================== ===
I've just been inspired by a webpage with 193 examples of "Hello World!" programs.
I want to make something a just slightly more useful for the beginning programmer. :)
Write the most concise, but clearly readable program (call it helloubuntu) according to this specification:- Say: "Hi! What's your name? "
- User enters name.
- Say: "Hello, (username)! Welcome to Ubuntu!"
You can also use a simple GUI, like a dialog box, to ask the question, input name, etc. Just make sure the code is elegant and easy-to-understand.
You can use programming languages, markup languages, shell scripts, or even SQL statements.
Try not to hog too many languages. Give other posters a fair go at posting their example too. If you are posting an alternative version of code for a language that has already been posted, mention that it is an alternative version.
I'll start off:
Ada (using normal fixed-length string)
Code:
with Ada.Text_IO;
use Ada.Text_IO;
procedure helloubuntu is
username : String(1..30);
lastchar : Integer;
begin
Put_Line("Hi! What's your name?");
Get_Line(username, lastchar);
Put_Line("Hello, " & username(1..lastchar) & "! Welcome to Ubuntu!");
end helloubuntu;
Ada (using the 2005 standard's unbounded-string)
Code:
with Ada.Text_IO, Ada.Text_IO.Unbounded_IO, Ada.Strings.Unbounded;
use Ada.Text_IO, Ada.Text_IO.Unbounded_IO, Ada.Strings.Unbounded;
procedure helloubuntu is
username : Unbounded_String;
begin
Put("Hi! What's your name? ");
Get_Line(username);
Put_Line("Hello, " & username & "! Welcome to Ubuntu!");
end helloubuntu;
Can we beat 193? :D
Re: "Hello Ubuntu" in every programming language
BASH
Code:
echo -n "Hi whats your name? "
read name
echo "Hello $name. Welcome to Ubuntu"
Re: "Hello Ubuntu" in every programming language
C
runningwithscissors came up with a better one. So here it is.
Code:
#include <stdio.h>
int main()
{
char name[64];
printf("Hi! What's your name? ");
scanf("%s", &name);
printf("Hello, %s! Welcome to Ubuntu (GNU)/Linux!\n", &name);
return 0;
}
That is my really bad C one. Please criticize me via PM if you feel the need.
Re: "Hello Ubuntu" in every programming language
Perl
Code:
Code:
#!/usr/bin/perl
use strict;
print "Hi, What is your name? ";
chomp( my $name = <STDIN> );
$name = ucfirst(lc($name)); # make "name" -> "Name"
print "Hello $name, Welcome to Ubuntu!\n";
Result:
Code:
[{bulldog}~]$./name.pl
Hi, What is your name? bULlDoG
Hello Bulldog, Welcome to Ubuntu!
[{bulldog}~]$
* Side note http://www.99-bottles-of-beer.net/ has examples of `99 Bottle of Beer' in every language that I can think of ( and alot that I couldn't ) ... Gives beginners some experience with conditionals and loops as well :)
Re: "Hello Ubuntu" in every programming language
NASM Assembly (i386)
It could be cleaner and more optimized, I was trying to make it easy to follow.
The command to assemble & link is at the top.
Code:
; nasm helloubuntu.asm -f elf && ld helloubuntu.o -o helloubuntu
section .bss
name resb 512
segment .text
; Constant data:
intro db "Hi! What's you're name? "
sz_intro equ $-intro
outro1 db "Hello "
sz_outro1 equ $-outro1
outro2 db "! Welcome to Ubuntu!", 10
sz_outro2 equ $-outro2
global _start
; Console in system call
console_in:
mov eax, 3
mov ebx, 0
int 0x80
ret
; Console out system call
console_out:
mov eax, 4
mov ebx, 1
int 0x80
ret
; Find first newline in input
get_len:
mov edx, 0
get_len_loop:
inc edx
cmp byte [name+edx], 10
jne get_len_loop
ret
; Start, equivalent to C's "main"
_start:
mov ecx, intro
mov edx, sz_intro
call console_out
mov ecx, name
mov edx, 512
call console_in
mov ecx, outro1
mov edx, sz_outro1
call console_out
mov ecx, name
call get_len
call console_out
mov ecx, outro2
mov edx, sz_outro2
call console_out
; System exit call
mov eax, 1
int 0x80
Re: "Hello Ubuntu" in every programming language
Python
Code:
#!/usr/bin/env python
print "Hello, " + raw_input("Hi! What's your name? ") + "! Welcome to Ubuntu!"
Re: "Hello Ubuntu" in every programming language
Improvements are welcome :D
I'm a beginner java programmer.
JAVA
Code:
import java.io.*;
public class HelloUbuntu {
public static void main(String[] args){
System.out.println("Hi! What's your name? \n");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
try{
userName = reader.readLine();
}
catch( IOException e ){
System.out.println("Ops! A problem!");
}
System.out.println("Hello, "+userName+" ! Welcome to Ubuntu!");
}
}
Re: "Hello Ubuntu" in every programming language
Pike
The Pike programming language: http://en.wikipedia.org/wiki/Pike_%2...ng_language%29
First, install Pike interpreter
$ apt-cache search pike
$ sudo apt-get install pike7.6
Code:
#!/usr/bin/env pike
// Hello from a Pike program
void say_this(string|void msg)
{
if (!msg)
write("Hello\n");
else
write(String.capitalize(msg) + "\n");
}
int main(int argc, array(string) argv)
{
string s = "hello" " " "ubuntu!";
mixed result = catch {
say_this(s);
};
// result will be != NULL string object if an exception was thrown.
if(result == 0)
write("Ok.\n");
else
write("There was an error: " + result + "\n");
return 0;
}
Save the code to "hello-ubuntu.pike" file.
Make it executable
$ chmod +x hello-ubuntu.pike
Run it
$ ./hello-ubuntu.pike
--------------------------------------------------------
EDIT: If you want ask user's name, then write
Code:
string name = Stdio.Readline()->read("Your name please:");
Re: "Hello Ubuntu" in every programming language
C++
Version 1: Do not write results to file
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
string username = "";
int main(int argc, char *argv[]);
int main(int argc, char *argv[]){
cout << "Hi! What's your name? ";
cin >> username;
cout << "Hello, " << username << "! Welcome to Ubuntu!\n";
return 0;
}
Version 2: Write results to file
Code:
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
string username = "";
int main(int argc, char argv[]);
int main(int argc, char *argv[]){
cout << "Hi! What's your name? ";
cin >> username;
cout << "Hello, " << username << "! Welcome to Ubuntu!\n";
ofstream userlist;
userlist.open ("userlist", ios::app);
userlist << username << "\n";
userlist.close();
return 0;
}
The second version keeps a log of users, one name per line.
Re: "Hello Ubuntu" in every programming language
PHP
Code:
#!/usr/bin/php
<?php
echo "Hi! Whats your name? ";
$name = ucfirst(trim(fgets(STDIN)));
echo "Hello, ".$name."! Welcome to ubuntu!\n";
?>
You need the php5-cli package to run this.