Repeater programming exercise.

The little snail shark wants to create a new programming language, and need your help with a function for his library.

Write a function that takes a string S and an integer N, as an input and repeats the string S, N times.

Example:
input ("b", 5) -> "bbbbb"
input ("Hello", 3) -> "HelloHelloHello"

Scroll down for the solution of this small coding problem. There will be a surprise there 😉

    public static string Repeater(string s, int n)
    {
        //What a nice small program
        //but so hard to explain in simple English ;)
        //But let me try.. First we shall declare a 
        //string variable. Then we will repeat a piece of
        //code "n" times. And every time we will save the string
        //"s" in our string variable. 
        string r = "";
        for (int k = 0; k < n; k++)
        {
            r += s;
        }
        //and then, when all this repeating is done
        //we return the content of "r".
        return r;
    }

But wait, don’t go yet. This function repeats a string but seems kind of messy “hellohellohello”. What if we want to have a option for separator between the two strings ? What if we want a function that sometimes get only two parameters as input, and sometimes three? Is this even possible ? In c# you can easily do that. You just make a second function with the same name but different input parameters! Like this one:

    public static string Repeater(string s, int n, char c)
    {
        if (n<1) return "";
        string r = "";
        for (int k = 0; k < n; k++)
        {
            r =r+s+c;
        }
//I am going to use Remove() to remove the last character
//form the string. The last ones number is r.Length - 1. Since 
//I don't know the length of the string.
//Open the Microsoft Visual Studio and try to see why I have to
//remove the last character. This problem often occurs so try
//to remember it. 
        r = r.Remove(r.Length - 1);
        return r;
    }

Now since we have both functions with the same name, you can write:

Console.WriteLine(Repeater(“Hello”,5));

or

Console.WriteLine(Repeater(“Hello”,5 “,”));

Cool, right : )

But this is a bit confusing , isn’t it. If we have many such functions, how would we know how many parameters we have to enter. Do we have to perfectly memorize all functions? And what if other want to read and use our code ? That is why we have documentation!

Try to write this before function:

    /// <summary>
    /// Yon have only two or three options for our function. 
    /// Two normal + one overload. 
    /// </summary>
    public static string Repeater(string s, int n)
    {
        //What a nice small program
        //but so hard to explain in simple English ;)
        //But let me try.. First we shall declare a 
        //string variable. Then we will repeat a piece of
        //code "n" times. And every time we will save the string
        //"s" in our string variable. 
        string r = "";
        for (int k = 0; k < n; k++)
        {
            r += s;
        }
        //and then, when all this repeating is done
        //we return the content of "r"
        return r;
    }

And then when you call it:

Console.WriteLine(Repeater(“Hello”,5));

Point your mouse over the name “Repeater”. You should be able see the text in the summary.

Now do some documentation before the second function.

And if you cannot see the overloads try this link:

https://stackoverflow.com/questions/9891656/visual-studio-how-to-show-overloads-in-intellisense