Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C#

(Uses Linq)

Date:11/19/10
Author:Sudipta
URL:sudipta.posterous.com
Comments:0
Info:n/a
Score: (2.90 in 21 votes)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _99Bottlez
{
    class Program
    {
        static void Main(string[] args)
        {
            int countOfBottles = 10;
            string lineTemplate = @"{X} bottles of beer on the wall, {X} bottles 
                                  of beer. Take one down and pass it around, {Y} 
                                  bottles of beer on the wall.";

            string lastLine =  @"No more bottles of beer on the wall, no more   
                               bottles of beer.Go to the store and buy some 
                               more, {X} bottles of beer on the wall.";

            List<string> songLines = new List<string> ();
            Enumerable.Range(1, countOfBottles)
                .Reverse()
                .ToList()
                .ForEach
                 (c => songLines.Add(lineTemplate.Replace("{X}", 
                 c.ToString()).Replace("{Y}", (c-1)!=0?(c - 1).ToString():@" No 
                 more bottles of beer on the wall.")));
            
            //Add the last line
            songLines.Add(lastLine.Replace("{X}", countOfBottles.ToString()));

            songLines.ForEach(c => Console.WriteLine(c));
            Console.ReadLine();
        }
    }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Concise, using C# 3.5 featuresJeff Dietrich10/26/0714
2Paul M. Parks04/20/059
C# Gratuitously FunctionalPaul Stancer04/14/091
corporate styleveteran corporate coder08/16/090
5Mark Hurley05/31/050
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050
v3.0 Easy Functional RecursiveYelinna Pulliti Carrasco08/04/090

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: