aaronvan icon

CameCaseCodeWars

aaronvan | PRO | 05/31/18 12:17:15 AM UTC | 0 ⭐ | 820 👁️ | Never ⏰ | []
C# |

2.11 KB

|

None

|

0 👍

/

0 👎

namespace CamelCase
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(CamelCase("mw  lupq  aiht jvzujptarsqxu i cur ndgycvec uvkcd"));
            Console.ReadKey();
        }
 
        public static string CamelCase(string str)
        {
            if (String.IsNullOrWhiteSpace(str))
                return str;
 
            // new string
            string newString = "";
 
            /* test first char. If white space... */
            if (str[0] == ' ')
            {
                // skip the white space and go to second letter in str, capitalize it and append to newString
                newString += char.ToUpper(str[1]);
 
                for (int j = 1; j < str.Length; j++)
                {
                    // skip any white space and capitalize the next letter
                    if (str[j] == ' ')
                    {
                        char b = str[j + 1];
                        newString += char.ToUpper(b);
                        j++;
                    }
                    else
                    {
                        char b = str[j];
                        newString += b;
                    }
                }
                return newString.Replace(" ", "");
            }
            /* test first char. If char is a letter... */
            else
            {
                // capitalize the first letter and append it to newString
                newString += char.ToUpper(str[0]);
                
        for (int j = 1; j < str.Length; j++)
                {
                    // skip any white space and capitalize the next letter
                    if (str[j] == ' ')
                    {
                        char b = str[j + 1];
                        newString += char.ToUpper(b);
                        j++;
                    }
                    else
                    {
                        char b = str[j];
                        newString += b;
                    }
                }
                return newString.Replace(" ", "");
            }
        }
    }
}

Comments