Proper naming programming exercise.

Name Array Capping programming exercise.

Way back in the 80 there ware all kind of strange jobs. For example banks had hundreds of millions of customers and most of them had names. But ware this names properly formatted ? Your jobs is to go back in time and solve the problem of improperly formatted names once and for all.

Write a method that properly format an array of “names”.

Example:
“bob”, “DAniel”, “DEXTER” => “Bob”, “Daniel”, “Dexter”

As a bonus you can remove all non alphabetical symbols.

Scroll down for the solution of this programming exercise.

The plan:

So, basically we need the first letter in a name to always be uppercased, and the rest to always be lowercased. Lets do this.

We will start, by creating a function that will take a string array, containing all the “names”, as an input. Then we can go through every “name” in the string array, and make the first letter uppercase and save it(in a string). Then also copy the rest of the letters, but as lowercased characters. Then add this newly constructed string, containing the properly formatted name to an array. And we do that with every word and in the end return the array with the proper names as the result.

    public static string[] FormatNames(string[] unformattedStrings)
    {
        //step 1
        List<string> output = new List<string>();
        //step 2
        foreach (string element in unformattedStrings)
        {
            //step 3
            if (element.Length > 0)
            {       
                //step 4
                string word = char.ToUpper(element[0]).ToString();
                for (int i = 1; i < element.Length; i++)
                {
                    word += char.ToLower(element[i]);
                }
                //step 5
                output.Add(word);
            }
        }
        //step 6
        return output.ToArray();
    }

Step1:
Lets reserve some memory for the result. I am going to use a List. This can be seen as a special type of an array. It represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. It is very convenient to use, and so, I often use it.

Step2:
We will use a “foreach” loop to go through all the items in the array “unformattedStrings”

Step3:
If somehow we get an empty string in “unformattedStrings” array, we can get an error, when we try to copy its FIRST element, and the go and copy the rest. Since there is no First element and so on. So if we get an empty string, we are going to nothing to it, and just copy it as it is.

Step4: First lets get the first character, of the element, of the array “unformattedStrings”. Then we shall convert it to and uppercased character using char.ToUpper() function, then ToString() make it a string.
And after that will copy the rest but making them lowercased.

Step5: We will add the newly formatted word in our list using one of the cool List functions Add(), which adds an object to the end of the list.

Step6: We will return the result, in form of an array, by converting our list using ToArray(), which copies the elements of the list to a new array.