0

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);
        }
AoLoim
  • 9
  • 3
  • Do you have any limitations on the problem? – Ermiya Eskandary Oct 24 '21 at 13:05
  • 2
    `char.ToUpper` returns a `char` - it doesn't do an in-place update – Ermiya Eskandary Oct 24 '21 at 13:05
  • Converting to uppercase, when you have just checked that it is a space, seems pointless. But you do need to uppercase the next char then – Hans Kesting Oct 24 '21 at 13:07
  • I'm adding 1 to i after I check the empty so it changes the next character. But if it just returns the value but doesn't replace it, should this be something like phrase[i] = char.ToUpper(phrase[i]); ? Don't have any limitations on this. – AoLoim Oct 24 '21 at 13:12
  • To process in-place you ned to transpose the output to a variable and then return that variable from the method. – Chris Schaller Oct 24 '21 at 13:22
  • Consider a more terse (and readable IMO) alternative with `string.join` and a `linq` query: `string.Join( " ", phrase.Split( ' ', StringSplitOptions.RemoveEmptyEntries ).Select( x => char.ToUpper( x[0] ) + x[1..] ) );` – Metro Smurf Oct 24 '21 at 18:01

3 Answers3

0

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.

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
0

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.

quain
  • 861
  • 5
  • 18
0

You can use the StringBuilder in very simple way as below

public static string ToJadenCase(string phrase)
        {
            if (phrase.Length == 0)
                Console.WriteLine("Empty string");

            string[] WordsList = phrase.Split();
            StringBuilder builder = new StringBuilder();
            foreach (var word in WordsList)
            {
                builder.Append(char.ToUpper(word[0]) + word.Substring(1)).Append(" ");
               
            }
            return builder.ToString();
        }