How do I change user text input to PascalCase?

Below code doesn't throw an error but instead of string it shows Dsystem.String[]Asystem.String

static void Main(string[] args)
    StringBuilder output =new StringBuilder();
    char greatLetter;
    string input;
    string[] words;
    Console.Write("Enter few words separatd by space:");
    input = Console.ReadLine();
    words = input.Split(' ');
    string[] cutWords = new string[words.Length];
    for (int i = 0; i <words.Length; i++)
        words[i] = words[i].ToLower();
        cutWords[i] = words[i].Substring(1);

3 answers

  • answered 2018-03-13 21:52 Sajeetharan

    You need to pass the index cutWords


  • answered 2018-03-13 21:59 Eser

    You can use TextInfo.ToTitleCase method

    string str = "abc def ghi";
    var titleCase = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);

    OUTPUT: Abc Def Ghi

  • answered 2018-03-13 22:12 maccettura

    Your code seems needlessly complex. Like @Eser said, you should be using ToTitleCase(). If you need the whitespace stripped out, then just strip it out

    public static void Main()
        string input = Console.ReadLine();
    public static string ToPascalCase(string input)
        input = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input);
        return new String(input.Where(c => !char.IsWhiteSpace(c)).ToArray());

    This also accounts for different whitespace delimiters, your code will not split properly if a different whitespace character is used...

    Fiddle here