Capitalize letters for an old lady.

Capitalize letters for an old lady, programming exercise.

An old lady, called “Baba Yaga”, wants capital words. She is very old and wants to write her memoirs, but she wants every word of hers to start with a capital letter.

Write a function that take string as an input and make a new string where:

All the first letters of a “word” are uppercased, and all other letters are lowercased.

Example:
input: "strIng oF Many wOrdS"
output: "String Of Many Words"

Scroll down for the solution of this easy coding problem for beginners.

The plan is to split the sentence into “words”. And then, go through every word and Copy the letters in the resulted string. If the letter is a first one we make it uppercased, else we make it lowercased.

    public static string BabaYagaFormat(string fulltext)
    {
        //step 1
        string result = "";
        string[] words = fulltext.Split(' ');

        //step 2
        foreach (string word in words)
        {
            for (int i = 0; i < word.Length; i++)
            {
                //step 3
                if (i == 0)
                {
                    result = result + char.ToUpper(word[i]);
                }
                else
                {
                    result += char.ToLower(word[i]);
                }
            }   
            //step 4            
            result += " ";
        }
        //step 5
        return result.Remove(result.Length - 1);
    }

Step 1
We will reserve some memory for building the output string.
And we will split the the string in to “words” by using Split(). Used this way, this method will split a string to substrings based on a character (on specified delimiting character).


Step 2
Now we can go to each of the resulted substrings using foreach, then we will go through each character in the substring but using a for loop.


Step 3
Inside this loop we will make the word upper or lowercased. And add it to the result. I will also demonstrate how += operator works. It just adds the content on the right side to the memory represented by the variable on the left side.


Step 4
We will have to add a empty space ‘ ‘ between the words.

Step 5
But since we always added an empty space ‘ ‘ on the end of each word, now we have to remove the last empty space. We can do that by using the Remove() method + the index(the position) we want to remove.

Now if you feel especially helpful, there is a request by the monster-under-the-bridge to make the opposite function. A function that takes a nicely formatted text, and add capital letters on random position.

input: "String Of Many Words"
output: "strIng oF maNy wOrdS"

Just a tip – if you need to use a random function, check out what this code do:

        for (int i = 0; i < 100; i++)
        {
            Random rnd1 = new Random();
            Console.Write(rnd1.Next(1, 10)+" ");
        }
        Console.WriteLine();
        Console.WriteLine("------------------------------");
        Random rnd2 = new Random();
        for (int i = 0; i < 100; i++)
        {
            Console.Write(rnd2.Next(1, 10) + " ");
        }