I have problem with my code. I don't know what am I doing wrong. I want for user to write some sentence and then program should split written sentence into single words where I use ToUpper on first chars of each word in array and then join the together again without spaces.
my code is:
static void Skrci(string stavek)
{
string[] p;
p = stavek.Split(' '); // polje separatov
for (int i = 0; i < p.Length; i++)
{
if (p[i].Length > 0)
{
char zacetnica = Char.ToUpper(p[i][0]);
p[i] = p[i].Remove(0, 1);
p[i] = p[i].Insert(0, zacetnica.ToString());
}
}
stavek = string.Join(" ", p);
}
static void Main(string[] args)
{
string[] p = null;
Console.Write("Vpiši nek stavek: ");
string stavek = Console.ReadLine();
Skrci(stavek);
Console.WriteLine(stavek);
Console.ReadKey(true);
}
I tried Step into to see if I can see what is wrong but I don't know.
You should pass your string to function as a reference, not as a value. Use
ref
keyword for that.Example: