DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
madstk1 profile image
Natamo

Not the best-looking of all, but it works. C#, with a lot of fiddling with Join and Select.

public static string CapitalizeFirstLast(this string input) => String.Join(" ", input.Split(" ").Select(sub => new string(String.Join("", sub.ToLower().ToCharArray().Select((v, k) => (k == 0 || k == sub.Length - 1) ? Char.ToUpper(v) : Char.ToLower(v))))));

And, to test it.

string[] testStrings = {
    "and still i rise",
    "when words fail music speaks",
    "WHAT WE THINK WE BECOME",
    "dIe wITh mEMORIEs nOt dREAMs",
    "hello"
};

foreach(string s in testStrings) {
    Console.WriteLine(s.CapitalizeFirstLast());
}