How do I call a method that I have defined?

tangara

Member
Joined
Feb 5, 2016
Messages
6
Programming Experience
1-3
Hi,

I am not sure why the method doesn't change the first letter of each word to Upper Case. Hope some one can help me. (So sorry the title should be this)

namespace ToUpperCaseOnFirstLetterOfWords
{
class Program
{
static void Main(string[] args)
{
string a = "institute of web technologies science";

ConvertToUpperCase(ref a);

}



public static String UppercaseFirstEach(string x) {


char[] s = x.ToLower().ToCharArray();
for (int i = 0; i < x.Count(); i++) // here a.Length can be a.Count(); both will achieve the same result
{
s = i == 0 || s[i - 1] == ' ' ? char.ToUpper(s) : s;

}
Console.WriteLine(x);
return x;
//return Console.WriteLine(x);
}


public static String ConvertToUpperCase(ref String a){
return UppercaseFirstEach(a);
}
}
}
 
Last edited:
Hi guys,

I found the solution.

Maybe this is too easy for you guys out there but hopefully this sharing will help someone struggling like me :)

namespace ToUpperCaseOnFirstLetterOfWords
{
class Program
{
static void Main(string[] args)
{
string a = "institute of web technologies science";

Console.Write(UppercaseFirstEach(a));

}



public static String UppercaseFirstEach(string x)
{

char[] array = x.ToCharArray();
// Handle the first letter in the string.
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array))
{
array = char.ToUpper(array);
}
}
}
return new string(array);
}

}
}
 
Back
Top Bottom