DEV Community

Kiron Roy
Kiron Roy

Posted on • Updated on

C# Capitalize First and Last Name

Here is a brief console application that asks the user for a first and last name and capitalizes the first letter of the first and last name respectively.

The developer needs to include the using statement: System.Globilzation. In addition, use the Trim() method to cut out extra spaces the user may type in.
Also \n creates a breakline without adding another Console.WriteLine statement.

And of course this is on Repl.it

Code below:

Main Method: (Entry point of the C# console application).
Two methods: One asks for user input, the other prints to the console.

using System;
using System.Globalization;

namespace _ReviewMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();

            string firstName = GetUsersName("What is your first name: ");
            string firstNameProperCaps = new CultureInfo("en-US")
                   .TextInfo
                   .ToTitleCase(firstName);

            string lastName = GetUsersName("\nWhat is your last name: ");
            string lastNamesProperCaps = new CultureInfo("en-US")
                   .TextInfo
                   .ToTitleCase(lastName);

            WelcomeUser(firstNameProperCaps,lastNamesProperCaps);

            Console.ReadLine();
        }

        private static void WelcomeUser(string firstName, string lastName)
        {
            Console.WriteLine($"\nHello {firstName} {lastName}");
        }

        private static string GetUsersName(string message)
        {
            Console.WriteLine(message);
            string output = Console.ReadLine().Trim();

            return output;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)