
Each word in a string can be uppercased. The words are separated by space characters. Each word should have an uppercase first letter. This is equivalent to the ucwords function from scripting languages. It provides a way to quickly process incorrectly capitalized names.

This program defines a method named UppercaseWords that is equivalent to the ucwords function in scripting languages such as PHP. The UppercaseWords method internally converts the string to a character array buffer.
The char array data is mutable, which means you can change it without reallocating or filling up the managed heap. The method mutates the character buffer and then returns a new string from the string constructor based on the mutated buffer.
This C# example program implements an algorithm that uppercases words in a string.
Program that uppercases first letters [C#]
using System;
class Program
{
static string UppercaseWords(string value)
{
char[] array = value.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[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
static void Main()
{
// Uppercase words in these strings.
const string value1 = "something in the way";
const string value2 = "dot net PERLS";
const string value3 = "String_two;three";
const string value4 = " sam";
// ... Compute the uppercase strings.
Console.WriteLine(UppercaseWords(value1));
Console.WriteLine(UppercaseWords(value2));
Console.WriteLine(UppercaseWords(value3));
Console.WriteLine(UppercaseWords(value4));
}
}
Output
Something In The Way
Dot Net PERLS
String_two;three
SamOverview. The program defines the UppercaseWords static method, which is equivalent to the ucwords function in scripting languages. Internally, the method body of the UppercaseWords method first tests the first character of the string. It special-cases the first character because the loop in the next part does not correctly allow for the very first character. The if-statement is validated in the test cases when the first two strings have their first letters uppercased.

For-loop. The next part of the UppercaseWords method in the program does a for-loop through all the characters in the string starting at the second character with index of one. In each iteration of the loop body, it tests the previous character for a space character. If it detects a space, it modifies the character array buffer to have an uppercase character.
For Loops
You might not be in the mood for deploying a custom method for uppercasing the first letter in each word in a string. You can use the ToTitleCase method on the TextInfo type, which does the same thing.
ToTitleCase MethodThis site contains more details and benchmarks on uppercasing the first letter in strings, but the other methods do not handle strings with multiple words embedded in them. The article demonstrates that using ToCharArray is one of the fastest methods for uppercasing specific letters.
Uppercase First Letter
Efficiency notes. The UppercaseWords method could be implemented by first splitting on the sub-words in the string based on a regular expression or the Split method, but this would result in many more allocations, one for the array reference data and one for each word. However, you could use the Regex.Split method for a better algorithm of breaking up the words.
Split String Examples Regex.Split Method Examples
We looked at an implementation of a method written in the C# language that uppercases all the words in a string's character data. The method uses two blocks to first handle the first character in the string data and then each character in the buffer that follows a space. The method does not contain sophisticated error correction and does not handle some words such as prepositions differently.
String Type