using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChangeCase
{
    public static class StringExtensions
    {
        // Convert the string to Pascal case.
        public static string ToPascalCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string capitalized.
            if (theString.Length < 2) return theString.ToUpper();

            // Split the string into words.
            string[] words = theString.Split(
                new char[] { },
                StringSplitOptions.RemoveEmptyEntries);

            // Capitalize and rejoin the words.
            string result = "";
            foreach (string word in words)
            {
                result +=
                    word.Substring(0, 1).ToUpper() +
                    word.Substring(1).ToLower();
            }
            return result;
        }

        // Convert the string to camel case.
        public static string ToCamelCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string in lowercase.
            if (theString.Length < 2) return theString.ToLower();

            // Convert to Pascal case.
            theString = theString.ToPascalCase();

            // Convert the first character to lowercase and return the string.
            return theString.Substring(0, 1).ToLower() + theString.Substring(1);
        }

        // With input a Pascal cased or camel cased string,
        // capitalize the first character and add a space before
        // each following capitalized letter.
        public static string ToProperCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string capitalized.
            if (theString.Length < 2) return theString.ToUpper();

            // Start with the first character capitalized.
            string result = theString.Substring(0, 1).ToUpper();

            // Add the remaining characters, inserting a space before capital letters.
            foreach (char ch in theString)
            {
                if (char.IsUpper(ch)) result += " ";
                result += ch;
            }
            return result;
        }
    }
}
