C# Program to Capitalize the First Character of Each Word in a String

Problem Statement:

Consider a scenario where you have a string and need to capitalize the first letter of each word in that string. For example, if the input string is “hello world,” the output should be “Hello World.”

C# Program to capitalize the first character of each word in a string

Solution Approach:

We’ll break down the problem into smaller steps and write a C# Program to Capitalize the first character of each word in a String.

Method 1: Capitalize the First Character of Each Word Using LINQ

We can split the input string into words, capitalize the first character of each word, and then join them back into a string. Here’s how we can achieve this using LINQ:

using System;
using System.Linq;

namespace CapitalizeWords
{
    class Program
    {
      /* C# Program to Capitalize a First letter of Each word in a string*/
        static void Main(string[] args)
        {
        string inputString = "hello world";
    Console.WriteLine($"Original string: { inputString}");
    Console.WriteLine($" Capitalized string: {CapitalizeWords(inputString)}");
        }

        public static string CapitalizeWords(string inputString)
        {
         // First, Split the input string into words and then capitalize the first character of each word,
        // and join them back
            return string.Join(" ", inputString.Split(' ').Select(word => char.ToUpper(word[0]) + word.Substring(1)));
        }
    }
}

Output:

Original string: hello world
Capitalized string: Hello World

Method 2: Using TextInfo.ToTitleCase()

Another approach is to use the TextInfo.ToTitleCase() method from the System.Globalization namespace. This method capitalizes the first character of each word in a given string:

using System;
using System.Globalization;

namespace CapitalizeWords
{
    class Program
    {
         /* Program to Capitalize a First letter of Each word in a string in C# */
        static void Main(string[] args)
        {

            string inputString = "hello world";
            Console.WriteLine($"Original string: { inputString}");
            string outputString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString.ToLower());

            Console.WriteLine($"Capitalized string: { outputString}");
        }
    }
}

Output:

Original string: hello world
Capitalized string: Hello World

Method 3: Using String Manipulation Functions

Let’s write a C# Program to capitalize the first character of each word in a string using manipulation functions.

using System;

class Program
{
      /* C# Program to Capitalize a First letter of Each word in a string*/
    static void Main(string[] args)
    {
        string input = "hello world";
        Console.WriteLine($"Original string:{input}"); 
        string[] words = input.Split(' ');

        for (int i = 0; i < words.Length; i++)
        {
            words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1);
        }

        string output = string.Join(" ", words);
        Console.WriteLine($"Capitalized string:{output}"); 
        // Output: Hello World
    }
}

Output:

Original string: hello world
Capitalized string: Hello World

Code Explanation:

  • We split the input string into an array of words using the Split function.
  • Then, we iterate over each word and capitalize its first character using char.ToUpper and string concatenation.
  • Finally, we join the modified words back into a string using string.Join.

Method 4: Using Regular Expressions

Now we will write a C# Program to capitalize the first character of each word in a string using regular expressions.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string input = "hello world";
        string output = Regex.Replace(input, @"\b\w", m => m.Value.ToUpper());
        Console.WriteLine(output); // Output: Hello World
    }
}

Code Explanation:

  • In the above code snippet, We have used a regular expression \b\w to match the first character of each word.
  • The Regex.Replace function replaces each match with its uppercase equivalent.

Recommended Articles:

Shekh Ali
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments