Home > Blockchain >  Characters to uppercase with C#. Jaden Casing Strings kata
Characters to uppercase with C#. Jaden Casing Strings kata

Time:10-25

This is my first question here. I'm trying to improve my basics by doing Code Wars exercises and I'm supposed to change the first character of every word to uppercase.

Example: This is my life now --> This Is My Life Now

This is my code at the moment but Uppercase doesn't seem to be working correctly. Why?

public static string ToJadenCase(string phrase)
        {
            for (int i = 0; i < phrase.Length; i  )
            {
                char _first = phrase[0];
                if (phrase[i] == ' ')
                {
                    i  ;
                    char.ToUpper(phrase[i]);
                }
                else if(phrase[i] == _first)
                {
                    char.ToUpper(phrase[i]);
                }
            }
            return phrase;
        }

Thank you all! from your answers, I was able to create a working method. Glad to join this kind of community.

My final code used a list to make this work, it's not pretty but it passed.

public static string ToJadenCase(string phrase)
        {
            List<char> _textlist = new List<char>();
            _textlist.Add(char.ToUpper(phrase[0]));

            for (int i = 1; i < phrase.Length; i  )
            {                  
                if (phrase[i] == ' ')
                {
                    _textlist.Add(phrase[i]);                        
                    _textlist.Add(char.ToUpper(phrase[i   1]));
                    i  ;
                }
                else
                {
                    _textlist.Add(phrase[i]);
                }                  
            }
            return string.Join("",_textlist);
        }

CodePudding user response:

There are multiple ways but your code is leaning towards array manipulation. We can use StringBuilder to assist with simple array based substitutions:

This is described here C# Replace or Remove Char by Index

public static string ToJadenCase(string phrase)
{
    // record the output as we process the input
    var output = new System.Text.StringBuilder(phrase);
    char _first = phrase[0];
    for (int i = 0; i < phrase.Length; i  )
    {
        if (phrase[i] == ' ')
        {
            i  ;
            output[i] = char.ToUpper(phrase[i]);
        }
        else if(phrase[i] == _first)
        {
            output[i] = char.ToUpper(phrase[i]);
        }
    }
    return output.ToString();
}

NOTE: I've only transposed your logic here, I give no guarantees that you'll pass the test cases with this, you should check for boundary conditions like a null or empty string passed in for phrase, or a string that starts with a space...

I'm also not sure what the comparison to the first char is for, so I wont comment on it, as a Code Wars or Hackerrank question I'll take your word for it that it is part of the requirement.

CodePudding user response:

If there is no specific perquisites, then this should do the trick:

public static void Main()
{
    var c = "to jaden case";        
    var str = c.Split(' ')
               .Select(x => x.Length > 1 ? Char.ToUpper(x[0])   x[1..] : x.ToUpper());
                
    var result = string.Join(' ', str);
    Console.WriteLine(result);
}

If needed, You could also, after string splitting, convert all pieces to lowercase if there could be a string with lower / upper case put in different places. There is also a thing about additional whitespace characters when joining multiple whitespaces, but for this I left it out.

  • Related