Text To HTML - C# Programming Exercise

In this exercise of C#, you need to create a class called TextToHTML, which should be able to convert several texts entered by the user into an HTML sequence. The goal is that, if the user enters the following:

Hello
It's me
I'm done

The class should convert it into the following HTML format:

Hello
It's me
I'm done

The class must contain:

- An array of strings to store the texts
- A method Add, to add a new text to the array
- A method Display, to show its content on the screen
- A method ToString, that returns a string containing all the texts, separated by "\n".

Additionally, you need to create an auxiliary class with a Main function to help you test the TextToHTML class functionality. This exercise is great for learning how to work with arrays of strings, class methods, and how to organize and display information in HTML format using C#. It’s an excellent practice for developing a better understanding of text manipulation and the use of methods in object-oriented programming.

 Category

OOP More On Classes

 Exercise

Text To HTML

 Objective

Create a class "TextToHTML", which must be able to convert several texts entered by the user into a HTML sequence, like this one:

Hola
Soy yo
Ya he terminado

should become

Hola

Soy yo

Ya he terminado

The class must contain:
An array of strings
A method "Add", to include a new string in it
A method "Display", to show its contents on screen
A method "ToString", to return a string containing all the texts, separated by "\n".
Create also an auxiliary class containing a "Main" function, to help you test it.

 Write Your C# Exercise

// Importing the System namespace to handle console functionalities and string manipulations
using System;
using System.Collections.Generic;

public class TextToHTML
{
    // An array to hold the strings entered by the user
    private List textList;

    // Constructor initializes the list to store strings
    public TextToHTML()
    {
        textList = new List();  // Using List instead of array for dynamic resizing
    }

    // Method to add a new string to the list
    public void Add(string newText)
    {
        textList.Add(newText);
    }

    // Method to display all the strings in the list, each on a new line
    public void Display()
    {
        // Loop through each string in the list and print it
        foreach (var text in textList)
        {
            Console.WriteLine(text);
        }
    }

    // Method to return the strings as a single string with newline separators
    public override string ToString()
    {
        // Join all strings with "\n" and return the result
        return string.Join("\n", textList);
    }
}

// Auxiliary class with the Main function to test the TextToHTML class
public class Program
{
    public static void Main()
    {
        // Create a new instance of TextToHTML
        TextToHTML textToHTML = new TextToHTML();

        // Adding some texts
        textToHTML.Add("Hola");
        textToHTML.Add("Soy yo");
        textToHTML.Add("Ya he terminado");

        // Displaying the contents of the TextToHTML object
        Console.WriteLine("Display Method Output:");
        textToHTML.Display();

        // Displaying the ToString output
        Console.WriteLine("\nToString Method Output:");
        Console.WriteLine(textToHTML.ToString());
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

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

  •  Class ScreenText

    In this exercise of C#, you need to create a class called ScreenText, which will display a certain text at specified screen coordinates. The class must have a ...

  •  Enhanced ComplexNumber class

    In this exercise of C#, you need to improve the ComplexNumber class by overloading the + and - operators to allow the addition and subtraction of...

  •  3D point

    In this exercise of C#, you need to create a class called Point3D to represent a point in 3-D space, with coordinates X, Y, and Z. The class must include the f...

  •  Catalog + Menu

    In this exercise of C#, you need to improve the catalog program so that the Main method displays a menu allowing the user to enter new data of any kind, as wel...

  •  Array of objects: table

    In this exercise, you need to create a class named "Table". This class should have a constructor that accepts the width and height of the table. ...

  •  House

    In this exercise, you need to create a class called "House" with an attribute called "area". The class should have a constructor to set the value of this attri...