Convert A Text File To Uppercase - C# Programming Exercise

This C# exercise involves creating a program that reads a text file and dumps its content into another file, making a transformation in the process. The transformation involves changing all lowercase letters to their uppercase equivalents. The goal of this exercise is to practice file handling and text manipulation in C#.

The program should read the original text file and iterate through its content, detecting lowercase letters and replacing them with their uppercase versions. Then, the program should save the result to a new text file. This exercise provides an excellent opportunity to become familiar with file handling as well as text conversion in C#. Additionally, as part of the instructions, the .cs file must contain a comment with your name to indicate that you have completed the task.

This exercise is perfect for those who want to learn how to work with string and file manipulation in C#, while also applying basic text operations efficiently.

 Category

File Management

 Exercise

Convert A Text File To Uppercase

 Objective

Write a program to read a text file and dump its content to another file, changing the lowercase letters to uppercase.

You must deliver only the ".cs" file, with you name in a comment.

 Write Your C# Exercise

// Importing the System namespace to access basic operations like Console and File handling
using System;  

// Importing the System.IO namespace to handle file operations
using System.IO;  

// Defining the ConvertToUppercase class
class ConvertToUppercase  
{
    // Method to convert the content of a text file to uppercase
    public static void ConvertFileToUppercase(string inputFileName, string outputFileName)
    {
        // Reading the content of the input file using File.ReadAllText
        string fileContent = File.ReadAllText(inputFileName);

        // Converting the content to uppercase using the ToUpper method
        string uppercasedContent = fileContent.ToUpper();

        // Writing the converted content to the output file using File.WriteAllText
        File.WriteAllText(outputFileName, uppercasedContent);

        // Displaying a message to the user that the file has been successfully written
        Console.WriteLine($"File content has been converted to uppercase and saved to {outputFileName}");
    }

    // Main method where execution starts
    static void Main(string[] args)
    {
        // Asking the user to input the file name for reading
        Console.Write("Enter the input file name (with extension): ");
        
        // Reading the file name from the user input
        string inputFileName = Console.ReadLine();  

        // Creating the output file name by appending "_uppercase" to the original file name
        string outputFileName = Path.GetFileNameWithoutExtension(inputFileName) + "_uppercase" + Path.GetExtension(inputFileName);

        // Calling the method to convert the content of the input file to uppercase and save it to the output file
        ConvertFileToUppercase.ConvertFileToUppercase(inputFileName, outputFileName);
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Convert any file to uppercase

    This C# exercise involves creating a program that reads a file of any kind and dumps its content into another file, applying a transformation in the process. The tran...

  •  File inverter

    This C# exercise involves creating a program to "invert" a file. The program must take an original file and create a new one with the same name but with the ".inv" ex...

  •  File encrypter

    This C# exercise involves creating a program to encrypt a text file into another text file. The program should be able to read the content of a text file, apply an en...

  •  Count words

    This C# exercise involves creating a program to count the number of words stored in a text file. The program should read the content of a text file, process it, and c...

  •  BMP width and height, BinaryReader

    This C# exercise involves creating a program to display the width and height of a BMP file using a BinaryReader. The program should read the BMP file header an...

  •  TXT to HTML translator

    This C# exercise involves creating a "Text to HTML converter". The program should read a source text file and create an HTML file from its contents. The destination f...