130
\$\begingroup\$

Your favourite programming language has just had a birthday. Be nice and sing it the Happy Birthday song.

Of course you should accomplish this by writing a program in that language. The program takes no input, and writes the following text to the standard output or an arbitrary file:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear [your favourite programming language]
Happy Birthday to You

You should substitute the bracketed part (and omit the brackets).

This is a code golf — shortest code wins.

UPDATE

I'm glad that the question aroused great interest. Let me add some extra info about scoring. As stated originally, this question is a code golf, so the shortest code is going to win. The winner will be picked at the end of this week (19th October).

However, I'm also rewarding other witty submissions with up-votes (and I encourage everybody to do so as well). Therefore although this is a code-golf contest, not-so-short answers are also welcome.

Results

Congratulations to Optimizer, the winner of this contest with his 42 byte long, CJam submission.

Leaderboard

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

/* Configuration */

var QUESTION_ID = 39752; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 48934; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

function answersUrl(index) {
  return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

var OVERRIDE_REG = /^Override\s*header:\s*/i;

function getAuthorName(a) {
  return a.owner.display_name;
}

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });

  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    var lang = a.language;
    if (/<a/.test(lang)) lang = jQuery(lang).text();
    
    languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
  });

  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);

  langs.sort(function (a, b) {
    if (a.lang > b.lang) return 1;
    if (a.lang < b.lang) return -1;
    return 0;
  });

  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<div id="language-list">
  <h2>Winners by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
16
  • 4
    \$\begingroup\$ Can we count the name of the programming language as one byte because it would only seem fair to people coding in a long-winded language such as JavaScript vs. someone doing it in C. We are really looking for most creative logic right? \$\endgroup\$
    – MonkeyZeus
    Oct 13, 2014 at 18:49
  • 16
    \$\begingroup\$ Why all the upvotes for this despite all the downvotes for the Bonbon song? This is just as boring a challenge. \$\endgroup\$
    – xnor
    Oct 14, 2014 at 7:15
  • 10
    \$\begingroup\$ I should probably mention that the song is copyrighted and distributing these programs may cause a DMCA \$\endgroup\$ Oct 14, 2014 at 14:38
  • 13
    \$\begingroup\$ guys! Happy Birthday song is now in public domain!! \$\endgroup\$
    – Optimizer
    Sep 23, 2015 at 10:01
  • 5
    \$\begingroup\$ At least one of the answers prints a leading newline. Is that allowed? \$\endgroup\$
    – Dennis
    Jun 19, 2016 at 21:32

208 Answers 208

1
2
3 4 5
7
5
\$\begingroup\$

Retina, 47 49 50 52 bytes

The program is encoded as ISO-8559-1 (which the official interpreter accepts). The leading newline is significant.


¶¶x¶

Happy Birthday to You
.{27}u
Dear Retina

Try it online.

Explanation

Step 1: Replace the (empty) input with “newline, newline, x, newline”.

Step 2: In between every character, insert Happy Birthday to You. We now have:

Happy Birthday to You
Happy Birthday to You
Happy Birthday to YouxHappy Birthday to You
Happy Birthday to You

Step 3: replace a 28-character string ending in u and not containing a newline with Dear Retina. The only occurrence of that is to YouxHappy Birthday to You.

\$\endgroup\$
5
\$\begingroup\$

Reng, 66 bytes

Noncompeting.

A"uoY ot yadhtriB yppaH"¤ao;A"gneR raeD yadhtriB yppaH ~"S0gO0gao;

Simple enough. A is 10, or a newline. "uoY ot yadhtriB yppaH" pushes that string, ¤ duplicates the string, ao; outputs everything. The second part is more interesting. "A"gneR raeD yadhtriB yppaH ~" pushes that string. S pushes 28, and 0g sets the character at (0, 28) to the top of stack, or a ~, the end program signal. This sets the last A to a tilde. Then, O0g sets the character at(0, 24) to a space/nop, removing the duplicate stack command. We print the third line using ao;. Then, we wrap around to the beginning of the line and print the last line again.

Here's a GIF, because who doesn't like those?

GIIIIF

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 36 bytes

”ŽØ¢©”©" to You"«Ð®" Dear 05AB1E"«s»

TIO Nexus

Try it online!

Output:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear 05AB1E
Happy Birthday to You

Explanation:

”ŽØ¢©”                                Push "Happy Birthday"
                                       STACK: ["Happy Birthday"]
      ©                               Store in register w/o popping
       " to You"«                     Append " to You"
                                       STACK: ["Happy Birthday to You"]
                 Ð                    Triplicate the string
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You"]
                  ®                   Push "Happy Birthday" from register
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You", "Happy Birthday"]
                   " Dear 05AB1E"«    Append " Dear 05AB1E"
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You", "Happy Birthday Dear 05AB1E"]
                                  s   Swap top two strings
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday Dear 05AB1E", "Happy Birthday to You"]
                                   »  Join by newlines
\$\endgroup\$
4
\$\begingroup\$

BBC BASIC, 81 / 76 ASCII characters

Download interpreter at http://www.bbcbasic.co.uk/bbcwin/bbcwin.html

Unfortunately, it's not possible to choose between strings within an expression, so the line endings are repetitively encoded in a DATA statement. On the other hand, when you read the last line, it does sound quite musical.

FORk=1TO4READa$PRINT"Happy Birthday ";a$:NEXT 
DATAto You,to You,Dear BASIC,to You

We can, however, choose what substring to select from a given string, for a saving of 5 characters (note that k=3 evaluates to -1 when true):

FORk=1TO4PRINT"Happy Birthday ";MID$("Dear BASICto You",11+(k=3)*10,10):NEXT

BBC BASIC, 69 ASCII characters

A shorter way:

a$="Happy Birthday "b$="to You"PRINTa$;b$'a$;b$'a$;"dear BASIC"'a$;b$
\$\endgroup\$
2
  • \$\begingroup\$ I had no idea that you didn't need a colon for one-liners... \$\endgroup\$
    – Beta Decay
    Oct 15, 2014 at 18:14
  • 1
    \$\begingroup\$ @BetaDecay according to the official syntax you do need the colon in most cases. But in practice BBC BASIC for Windows lets you get away without them. The original BBC Micro manual gave certain advice for improving the speed of your programs, of which I've taken in the following: use lowercase variable names, and eliminate as many unnecesary characters as possible. \$\endgroup\$ Oct 15, 2014 at 18:32
4
\$\begingroup\$

J (44)

2|.'Happy birthday ',"1'dear J',>3#<'to you'
\$\endgroup\$
4
\$\begingroup\$

Groovy (64)

4.times{println"Happy Birthday ${it==2?'Dear Groovy':'to You'}"}
\$\endgroup\$
4
\$\begingroup\$

VBA 99

Why VBA? Why not?

Sub H(): For i = 0 To 3: MsgBox "Happy Birthday " & IIf(i = 2, "Dear VBA", "to You"): Next: End Sub

Ungolfed

Sub H()
    For i = 0 To 3
        MsgBox "Happy Birthday " & IIf(i = 2, "Dear VBA", "to You")
    Next
End Sub
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Still shorter than C# :D \$\endgroup\$
    – Brandon
    Oct 17, 2014 at 12:22
  • \$\begingroup\$ For what it's worth (2.5yrs later), you can remove the auto-formatting to get it down to 86 bytes: Sub H():For i=0To 3:MsgBox"Happy Birthday " &IIf(i=2,"Dear VBA","to You"):Next:End Sub \$\endgroup\$ Apr 27, 2017 at 13:03
  • \$\begingroup\$ @EngineerToast I don't feel like that's legit. The only environment that compiles that code enforces those spaces. \$\endgroup\$
    – RubberDuck
    Apr 27, 2017 at 13:31
  • 1
    \$\begingroup\$ I used to think that, too, so I did some searching on meta and found a post that made sense to me saying why autoformatting should be discounted. It's not wrong either way but I somewhat enjoy finding all the white space I can remove from VBA and have it still work. \$\endgroup\$ Apr 27, 2017 at 14:02
  • \$\begingroup\$ @EngineerToast I'll actually buy that, because the official grammar doesn't require the white space. \$\endgroup\$
    – RubberDuck
    Apr 27, 2017 at 14:05
4
\$\begingroup\$

Common Lisp (62 60 chars)

(format t"~@{Happy Birthday ~[to You~;dear CL~]~%~}"0 0 1 0)

Output

Happy Birthday to You
Happy Birthday to You
Happy Birthday dear CL
Happy Birthday to You
\$\endgroup\$
2
  • \$\begingroup\$ Is the space before and after the string needed? \$\endgroup\$
    – Kijewski
    Oct 21, 2014 at 1:40
  • \$\begingroup\$ @Kay No, the space is not needed. I removed it, which saves 2 chars. Thanks. \$\endgroup\$
    – coredump
    Oct 21, 2014 at 5:58
4
\$\begingroup\$

Casio CFX-9850G, 121 bytes 96

Just for the fun of it, the Happy Birthday song for the CFX-9850G graphing calculator.

Some instructions are just displayed as multiple bytes, but as far as I know they are encoded as 1 or 2 bytes. I marked them with colors. Edit: I checked the memory, so the program uses 121 bytes in the calculator.

The song text would not fit in a line, especially line 3 because in text mode the calculator can only display 7 rows and 21 columns. So I print the lyrics in graphing mode, which uses a tiny font.

Happy Birthday, CFX-9850G. Program

Here is the output:

Happy Birthday CFX-9850G. Output

\$\endgroup\$
4
\$\begingroup\$

Qt 5.4 qmake pro file, 119 bytes

Since the winner has already been announced, I thought I'd give a try for the witty category.

O=$$escape_expand(\n)
A=Happy Birthday
B=$$A to you$$O
CONFIG(debug,debug|release){log($$A dear qmake$$O)}log($$B)

This abuses qmake horribly.

qmake under default settings will parse a script 3 times over. For debug, release, and a build_pass. So we just let it throw out the "Happy Birthday" for all logs, with an extra "To you..." when a debug makefile is being generated.

Happy Birthday to you
Happy Birthday to you
Happy Birthday dear qmake
Happy Birthday to you
\$\endgroup\$
4
\$\begingroup\$

Java, 127 125 bytes

class h{public static void main(String[]a){String b="\nHappy Birthday ",d=b+"to You";System.out.print(d+d+b+"Dear Java"+d);}}

However, if you ignore all of the excess stuff, it is only 82 80 bytes:

String b="\nHappy Birthday ",d=b+"to You";System.out.print(d+d+b+"Dear Java"+d);

Oh, and it starts with a newline, saving 2 bytes.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ output is not exactly as required, since there's an extra line at the start \$\endgroup\$
    – Ky -
    Oct 17, 2014 at 1:13
  • 3
    \$\begingroup\$ @Supuhstar It merely says to output the song happy birthday. It makes no requirement for excess newlines/whitespace. \$\endgroup\$ Oct 17, 2014 at 2:56
  • \$\begingroup\$ it says "writes the following text to the standard output or an arbitrary file:"... so... maybe you could stretch that to include extra bits? It doesn't sound like this is the intended answer, though. \$\endgroup\$
    – Ky -
    Oct 17, 2014 at 3:02
  • \$\begingroup\$ I'm not exactly sure what you mean, but System.out.println is a standard output. \$\endgroup\$ Oct 17, 2014 at 3:04
  • \$\begingroup\$ I don't mean the output method, but the actual output, which starts with a blank line \$\endgroup\$
    – Ky -
    Oct 17, 2014 at 3:14
3
\$\begingroup\$

JavaScript, 74 73

for(i=5;--i;)console.log('Happy birthday',i-2?'to you':'dear JavaScript')

Thanks to Dancrumb for the byte-saving tip. It would be 68 67 bytes if I could use 4 alerts instead of newlines.

for(i=5;--i;)alert('Happy birthday',i-2?'to you':'dear JavaScript')
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can shave off a byte with: for(i=5;--i;)console.log('Happy birthday',i-2?'to you':'dear JavaScript') \$\endgroup\$
    – Dancrumb
    Oct 13, 2014 at 16:15
  • 1
    \$\begingroup\$ If you want alert(), then use only 1, based on my comment at the other JavaScript answer. That will be 71 character without alert() flood. \$\endgroup\$
    – manatwork
    Oct 13, 2014 at 16:26
  • \$\begingroup\$ You could also avoid the loops and let the output just be the expression itself (which is true on newer consoles): (a="\nHappy Birthday")&&(b=a+" to You")&&b+b+a+" Dear JavaScript"+a (67 too). \$\endgroup\$ Oct 18, 2014 at 19:19
3
\$\begingroup\$

Swift, 83

Run this in Xcode Playground

let h="\nhappy birthday"
let t=" to you"
let f=h+t+h+t+h+" dear Swift"+h+t
print(f)
\$\endgroup\$
2
  • \$\begingroup\$ If you put the space after happy birthday, you can get rid of the two in to you and dear Swift, saving you one byte \$\endgroup\$
    – Beta Decay
    Oct 15, 2014 at 6:11
  • \$\begingroup\$ If you declare all 3 variables on the same line, you can save 8 bytes. \$\endgroup\$
    – NobodyNada
    Oct 18, 2014 at 20:56
3
\$\begingroup\$

Lua, 96 bytes

I was trying to be cool about this and make use of all the best Lua tricks I know but the guy who wrote a simple for loop with an if i==3 beat me by 20+ bytes >.<

Still, I think this is pretty cool in terms of Golfing:

a='Happy Birthday to you\ndear Lua\n'print(a:sub(0,-10):rep(3):sub(0,-8)..a:rep(2):sub(23,-10))

I was looking at recursively building the string a from a number using string.char() but I couldn't compress it anywhere near the length of a='Happy Birthday to you\ndear Lua\n'

\$\endgroup\$
3
\$\begingroup\$

Powershell one liner: 73 71

1..4|%{"Happy Birthday $(if($_-eq3){"Dear Powershell"}else{"to You"})"}

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You could shrink this to 71 if you did $_-eq3 \$\endgroup\$ Oct 16, 2014 at 13:06
3
\$\begingroup\$

APL: 45

4 23⍴'Dear APL',⍨61⍴'Happy birthday to you  '

Explanation:

61⍴ reapeats the string up to length 61, after which "Dear APL" is concatenated (,)

Note that ⍨ inverts arguments, so it's added at the end.

4 23⍴ just makes a matrix 4x23 using the string on the right, starting from the beginning of the string each time it reaches the end.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 42

V4+"Happy Birthday "?"to You"nN2"Dear Pyth

Demonstration:

$ pyth -c 'V4+"Happy Birthday "?"to You"nN2"Dear Pyth'
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear Pyth
Happy Birthday to You

How it works:

V4 sets up a for loop, with N iterating over 0, 1, 2, 3.

Then, we create the string starting with "Happy Birthday ", and ending with "to You" if N does not equal 2 (nN2 calculates this), or ending with "Dear Pyth" otherwise.

Exact tie with CJam - language name and character count.


Alternative, 43 character solution:

J"Happy Birthday to You"JJ+<J15"Dear Pyth"J

It's one character longer because it needs an end quote.

\$\endgroup\$
4
  • \$\begingroup\$ Aren't the V and N implemented after the question was asked ? \$\endgroup\$
    – Optimizer
    Oct 21, 2014 at 19:38
  • 1
    \$\begingroup\$ @Optimizer No, they were added a while ago. Looking back at the commit log, they were added on August 21st, in a commit called: Pyth 2.1.0 - Changes to -;\AcPUVXz Out of curiosity, what gave you that impression? \$\endgroup\$
    – isaacg
    Oct 21, 2014 at 19:50
  • \$\begingroup\$ Your recent Pyth tips answer left me thinking that it is something new and u always keep saying that you keep on adding features :). \$\endgroup\$
    – Optimizer
    Oct 21, 2014 at 19:56
  • \$\begingroup\$ @Optimizer Ah - while I do add new things, I'm also trying to give general programming tips on that page, which cover everything in Pyth. \$\endgroup\$
    – isaacg
    Oct 21, 2014 at 20:21
3
\$\begingroup\$

Julia, 75 67 65 characters

a,b,c="Happy Birthday ","To You\n","Dear Julia\n";print("$a$b$a$b$a$c$a$b")

a="Happy Birthday";b=("$a To You\n");print("$b$b$a Dear Julia\n$b")

a="Happy Birthday To You\n";print("$a$a$(a[1:15])Dear Julia\n$a")

Just for fun, here is an ungolfed version, with a loop (114 characters):

for i = 1:4
    a,b,c = "Happy Birthday ","To You","Dear Julia"
    i != 3 ? println("$a$b") : println("$a$c")
end
\$\endgroup\$
3
\$\begingroup\$

Bubblegum, 42 bytes

0000000: f3 48 2c 28 a8 54 70 ca 2c 2a c9 48 49 ac  .H,(.Tp.,*.HI.
000000e: 54 28 c9 57 88 cc 2f e5 22 4a d4 25 35 b1  T(.W../."J.%5.
000001c: 48 c1 a9 34 29 29 27 35 bd 34 17 bb 1e 00  H..4))'5.4....

Note that this answer is non-competing, since Bubblegum is a lot younger than this challenge.

\$\endgroup\$
3
\$\begingroup\$

Fishing, 116 bytes

v+CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  `Happy Birthday `P{`to You`N}P{N}P{{`Dear Fishing`N}}P{N
\$\endgroup\$
1
  • \$\begingroup\$ I've posted a few questions, but here's my first answer! \$\endgroup\$
    – Arcturus
    Oct 9, 2015 at 23:25
3
\$\begingroup\$

Seriously, 59 57 bytes (non-competitive)

"Happy Birthday to You";"Happy Birthday Dear Seriously"@;

Explanation

Push "Happy Birthday to You", duplicate, push "Happy Birthday Dear Seriously", rotate the two top elements, duplicate, and implicit popping and printing.

\$\endgroup\$
1
  • \$\begingroup\$ 57 bytes: "Happy Birthday to You";"Happy Birthday Dear Seriously"@; \$\endgroup\$
    – user45941
    Jan 8, 2016 at 4:24
3
\$\begingroup\$

///, 48 bytes

/U/Happy Birthday //T/Uto You/T
T
UDear \/\/\/
T
\$\endgroup\$
3
\$\begingroup\$

Vim, 90 45 bytes

i
Happy Birthday to you
[ESC]
yypppPWWd$adear Vim

90 chars version:

i
Happy Birthday to you dear Vim
[ESC]
:%s/\(.* .* \)(.. .*\) \(.* .*\)/\1\2\r\1\2\r\1\3\r\1\2

I'm certain there is a quicker way but this is what I just came up with :)

Puts out

Happy Birthday to you
Happy Birthday to you
Happy Birthday dear Vim
Happy Birthday to you
\$\endgroup\$
3
\$\begingroup\$

Underload, 59 Bytes

(to You
):(Happy Birthday ):S~S:S~:S~:S(Dear Underload
)SSS

I was hindered by Underload's lack of stack manipulation commands. I had to push stuff in a strange order and swap a bit more than I would have preferred, but oh well...

Stack trace:

(...) | (to You)
:     | (to You)(to You)
(...) | (to You)(to You)(Happy Birthday )
:S    | (to You)(to You)(Happy Birthday )         ; output "Happy Birthday "
~     | (to You)(Happy Birthday )(to You)
S     | (to You)(Happy Birthday )                 ; output "to You"
:S    | (to You)(Happy Birthday )                 ; output "Happy Birthday "
~     | (Happy Birthday )(to You)
:S    | (Happy Birthday )(to You)                 ; output "to You"
~     | (to You)(Happy Birthday )
:S    | (to You)(Happy Birthday )                 ; output "Happy Birthday "
(...) | (to You)(Happy Birthday )(Dear Underload)
S     | (to You)(Happy Birthday )                 ; output "Dear Underload"
S     | (to You)                                  ; output "Happy Birthday "
S     |                                           ; output "to You
\$\endgroup\$
3
\$\begingroup\$

Brainfuck, 593 characters

Used this

++++++++++[>+>+++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>++.>>+++++++.>>++..>+.<<<<<<<++.>++++++.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>+++++++++.>>>.++++++.<<<<<<<.>>>.>>.>>-----..>.<<<<<<<.>.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>.>>>.++++++.<<<<<<<.>>>.>>.>>-----..>.<<<<<<<.>.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>++.>>>>+.<.>>--.<<<<<<.>--.>>>>>.<<.>++++.>----.<---.>+++++++.<<++.>+++++.<<<<<<.>>>.>>--.>>-----..>.<<<<<<<.>.>>>>--.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>.>>>.++++++.
\$\endgroup\$
3
\$\begingroup\$

C++, 135 bytes

Great thanks to Steadybox and DJMcMayhem for shortening this code by 55 characters! :)

#include<iostream>
#include<string>
std::string s="Happy Birthday ",t="to you\n",u="Dear C++\n";int main(){std::cout<<s+t+s+t+s+u+s+t;}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can use + instead of <<. \$\endgroup\$
    – DJMcMayhem
    Jan 11, 2018 at 17:58
  • 1
    \$\begingroup\$ Welcome to PPCG! You can remove the space from between #include and <headerName>. Also, you can declare the strings in one go so you don't have to repeat the string. (After that using the std:: prefix is shorter than using namespace std;, too). Like this. \$\endgroup\$
    – Steadybox
    Jan 11, 2018 at 18:11
3
\$\begingroup\$

Japt, 41 38 bytes

`Happy Bir»y  Y
`
³h58Ui` De Japt

Test it

\$\endgroup\$
2
\$\begingroup\$

Lua: 91 72 characters

First pass:

for i=1,4 do io.write'Happy Birthday 'if i==3 then print'Dear Lua'else print'to You'end end

Demo

Even smaller:

for i=1,4 do print('Happy Birthday '..(i==3 and'Dear Lua'or'to You'))end

Demo

TI-BASIC: 65

:PROGRAM:H
:"HAPPY BIRTHDAY TO YOU
:Disp Ans,Ans,sub(Ans,1,15)+"DEAR TI-BASIC
:Disp Ans

In TI-BASIC, all built-in commands (even multi letter ones) are either one or two bytes on the calculator. The size I used represents the (minimum, since names take space) space taken by this code on my TI-84+SE running OS version 2.53MP. Seemingly wrong syntax (unclosed strings) is intentional.

TI-68k: 90

:h()
:Prgm
:"Happy Birthday to You"→a
:Disp a,a,mid(a,1,15)&"Dear TI-68k",a
:EndPrgm

Again, the size I used is the minimum real size on the calc (in this case, a TI-92).

\$\endgroup\$
2
\$\begingroup\$

R, 69 chars

for(i in 1:4)cat('Happy Birthday',ifelse(i==3,'Dear R','to You'),'\n')

Or

for(i in 1:4)cat('Happy Birthday',if(i==3)'Dear R' else 'to You','\n')

All output:

Happy Birthday to You 
Happy Birthday to You 
Happy Birthday Dear R 
Happy Birthday to You 

Otherwise with 66 characters there was, without the for loop:

a=rep('to You',4);a[3]='Dear R';cat(paste('Happy Birthday',a,'\n'))

But it outputs:

Happy Birthday to You 
 Happy Birthday to You 
 Happy Birthday Dear R 
 Happy Birthday to You 
\$\endgroup\$
2
\$\begingroup\$

C#

LINQPad (69):

string x="\nHappy Birthday ",y=x+"to You";(y+y+x+"Dear C#"+y).Dump();

Console (75):

string x="\nHappy Birthday ",y=x+"to You";Console.Write(y+y+x+"Dear C#"+y);

Ungolfed:

string x = "\nHappy Birthday ";
string y = x + "to You";

//LINQPad
(y + y + x + "Dear C#" + y).Dump(); 

//Console
Console.Write(y+y+x+"Dear C#"+y);
\$\endgroup\$
1
2
3 4 5
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.