C# To Pascal Converter - C# Programming Exercise

This exercise involves creating a C# program that converts simple C# programs to their equivalent in the Pascal language. The program should read C# code, identify its elements (such as variable declarations, control structures, functions, etc.), and generate Pascal code that represents the same logic. The program should handle basic structures like variables, conditionals, and loops, and produce Pascal code that compiles correctly. This exercise involves parsing and transforming code from one programming language to another.

 Category

File Management

 Exercise

C# To Pascal Converter

 Objective

Create a program that converts simple C# programs, such as the following one, to the Pascal language.

 Write Your C# Exercise

// Importing the System namespace to use basic functionality like Regex
using System;

class Program // Class definition in C#
{
    // Main method where the program execution begins
    static void Main()
    {
        // Example C# code to convert
        string cSharpCode = @"
using System;

class Program
{
    static void Main()
    {
        int x = 10;
        int y = 5;
        if (x > y)
        {
            Console.WriteLine(""x is greater"");
        }
        else
        {
            Console.WriteLine(""y is greater"");
        }
    }
}
";

        // Convert the C# code to Pascal
        string pascalCode = ConvertCSharpToPascal(cSharpCode);

        // Print the Pascal code to the console
        Console.WriteLine("Converted Pascal Code:");
        Console.WriteLine(pascalCode);
    }

    // Method to convert simple C# code to Pascal code
    static string ConvertCSharpToPascal(string cSharpCode)
    {
        // Remove 'using System;' line from C#
        // This line removes the unnecessary 'using System;' from the C# code
        cSharpCode = Regex.Replace(cSharpCode, @"using\s+System;\s*", "");

        // Convert C# class declaration to Pascal program declaration
        // This converts the C# 'class Program' to Pascal's 'program Program;'
        cSharpCode = Regex.Replace(cSharpCode, @"class\s+(\w+)", "program $1;");

        // Convert C# method declaration to Pascal method declaration
        // This converts the C# 'static void Main()' to Pascal's 'begin'
        cSharpCode = Regex.Replace(cSharpCode, @"static\s+void\s+Main\(\)", "begin");

        // Convert C# variables declaration to Pascal variable declaration
        // This converts C# declarations like 'int x = 10;' to Pascal's 'var x: Integer; begin x := 10;'
        cSharpCode = Regex.Replace(cSharpCode, @"int\s+(\w+)\s*=\s*(\d+);", "var $1: Integer; begin $1 := $2;");

        // Convert C# if-else block to Pascal if-else block
        // This converts C# 'if (condition) { ... } else { ... }' to Pascal's 'if condition then ... else ...'
        cSharpCode = Regex.Replace(cSharpCode, @"if\s*\((.*?)\)\s*{(.*?)}\s*else\s*{(.*?)}", "if $1 then\n$2\nelse\n$3");

        // Convert C# method calls like Console.WriteLine to Pascal WriteLn
        // This converts the C# method 'Console.WriteLine()' to Pascal's 'WriteLn()'
        cSharpCode = Regex.Replace(cSharpCode, @"Console\.WriteLine\((.*?)\);", "WriteLn($1);");

        // Add an 'end.' to complete the Pascal program
        // This adds 'end.' at the end of the Pascal code to indicate the end of the program
        cSharpCode = cSharpCode + "\nend.";

        // Return the final Pascal code
        return cSharpCode;
    }
}

 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#.

  •  Dump

    This exercise involves creating a "dump" utility: a hex viewer that displays the contents of a file, with 16 bytes per row and 24 rows per screen. The program should pause a...

  •  DBF extractor

    This exercise involves creating a program that displays the list of fields stored in a DBF file. The DBF format is used by the old dBase database manager and is still suppor...

  •  Text censorer

    This exercise involves creating a program to censor text files. The program should read a text file and output its contents to a new file, replacing certain words with "[CEN...

  •  SQL to text

    In this exercise, you need to create a C# program capable of parsing SQL INSERT commands and extracting their data into separate lines of text. The program should pro...

  •  PGM viewer

    The PGM format is one of the versions of the NetPBM image formats. Specifically, it is the variant capable of handling images in shades of gray. Its header starts with a line conta...

  •  Display BMP on console V2

    In this exercise, you are asked to develop a program in C# that can display a 72x24 BMP file on the console. To do this, you must use the information contained...