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);
}