Mohanapriya R Mohanapriya R
Updated date Feb 15, 2024
In this article, we will learn how to convert uppercase characters to strings in C#. Includes a sample program and output for easy understanding.

Converting Uppercase to String in C#

When working with strings in C#, you may encounter scenarios where you need to convert uppercase characters to strings. This could be for various reasons, such as formatting user input or processing data from external sources. In this guide, we will learn the process of converting uppercase characters to strings in C# with a simple program.

Before we dive into the code, let's understand the basic concept behind converting uppercase characters to strings. In C#, each character is represented by a Unicode value, and uppercase characters have distinct Unicode values compared to their lowercase counterparts. Therefore, converting uppercase characters to strings involves iterating through each character in the input string and converting any uppercase characters to their corresponding string representations.

Now, let's take a look at a simple C# program that demonstrates how to achieve this:

using System;

class Program
{
    static void Main(string[] args)
    {
        string input = "HELLO WORLD";
        string output = ConvertUppercaseToString(input);
        Console.WriteLine("Converted String: " + output);
    }

    static string ConvertUppercaseToString(string input)
    {
        string result = "";
        foreach (char c in input)
        {
            if (char.IsUpper(c))
            {
                result += c.ToString();
            }
        }
        return result;
    }
}

In this program, we have a method named ConvertUppercaseToString that takes an input string and returns a string containing only the uppercase characters. Inside the method, we iterate through each character in the input string using a foreach loop. For each character, we use the char.IsUpper method to check if it is an uppercase character. If it is, we append it to the result string.

Now, let's run this program and see the output:

Converted String: HELLOWORLD

As you can see, the program successfully converted the uppercase characters in the input string "HELLO WORLD" to a new string "HELLOWORLD".

In conclusion, converting uppercase characters to strings in C# is a straightforward process that involves iterating through each character in the input string and appending the uppercase characters to a new string. By understanding the concepts and utilizing the provided sample program, you can easily implement this functionality in your C# projects.

Comments (0)

There are no comments. Be the first to comment!!!