Convert Text to Uppercase and Save to New File in C#

This program is designed to read a given text file, convert all lowercase letters to uppercase, and then save the modified content to a new file. The program will take an input file specified by the user, process it, and generate an output file with the same name as the original, but with an added ".uppercase" extension. The transformation of the text ensures that every lowercase letter in the original file is converted to uppercase.



Group

File Handling in C#

Objective

1. The user is asked to input the name of the text file they want to process.
2. The program reads the content of the file.
3. Every lowercase letter in the content is converted to uppercase.
4. The program then saves the transformed text to a new file with the ".uppercase" extension.
5. Proper error handling should be included for situations where the file cannot be found or read.

Write a program to read a file (of any kind) and dump its content to another file, changing the lowercase letters to uppercase.

Example C# Exercise

 Copy C# Code
using System;
using System.IO;

class TextFileUppercase
{
    // Main method to execute the program
    static void Main(string[] args)
    {
        // Ask the user for the input file name
        Console.WriteLine("Please enter the name of the file to read:");

        // Read the name of the input file from the user
        string inputFileName = Console.ReadLine();

        // Generate the name of the output file by appending ".uppercase"
        string outputFileName = Path.GetFileNameWithoutExtension(inputFileName) + ".uppercase" + Path.GetExtension(inputFileName);

        try
        {
            // Read all content from the input file
            string content = File.ReadAllText(inputFileName);

            // Convert all characters to uppercase
            string upperCaseContent = content.ToUpper();

            // Write the uppercase content to the output file
            File.WriteAllText(outputFileName, upperCaseContent);

            // Notify the user that the operation was successful
            Console.WriteLine($"The content has been successfully saved to {outputFileName}");
        }
        catch (FileNotFoundException)
        {
            // If the file is not found, inform the user
            Console.WriteLine("Error: The specified file could not be found.");
        }
        catch (Exception ex)
        {
            // Handle any other unexpected errors
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

 Output

//If the input file data.txt contains:
this is a simple example of text content.

//The output file data.uppercase.txt will contain:
THIS IS A SIMPLE EXAMPLE OF TEXT CONTENT.

Share this C# Exercise

More C# Practice Exercises of File Handling in C#

Explore our set of C# Practice 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#.

  • Invert File Content and Save in Reverse Order in C#

    This program is designed to read a binary file and create a new file with the same name but with a ".inv" extension. The new file will contain the same bytes as the original file, ...

  • Text File Encryption Program in C#

    This program is designed to encrypt a text file and save the encrypted content into another text file. The encryption method used here is a simple character shifting technique, whe...

  • Word Count Program for Text File in C#

    This program is designed to read a text file and count the number of words it contains. A word is considered any sequence of characters separated by spaces, newlines, or punctuatio...

  • Display BMP File Width and Height using BinaryReader in C#

    This program is designed to read a BMP (Bitmap) image file and display its width and height. The BMP file format has a specific header structure, which contains important informati...

  • File Text to HTML Converter in C#

    This program is a "Text to HTML Converter" that reads the contents of a source text file and converts it into an HTML file. The program will take the text file and process its cont...

  • File Inverter Using FileStream in C#

    This program takes a file and creates a new file where the bytes are inverted. The program reads the contents of the original file using a FileStream, then writes these contents to...