Text to HTML Learn programming C#

Lesson:

OOP More On Classes


Exercise:

Text to HTML


Objetive:

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.


Code:

using System;
class TextToHTML
{

    protected string[] myHTML;
    protected int maxLines = 1000;
    private int counter = 0;

    public TextToHTML()
    {
        myHTML = new string[maxLines];
    }


    public void Add(string newSentence)
    {
        if (counter < maxLines)
        {
            myHTML[counter] = newSentence;
            counter++;
        }
    }


    public string ToString()
    {
        string allHTML = "\n\n";

        for (int i = 0; i < counter; i++)
        {
             allHTML += myHTML[i]; allHTML += "\n";
        }

        allHTML += "\n";
        allHTML += "\n";

        return allHTML;
    }

    public void Display()
    {
        Console.Write(ToString());
    }

}

class TextTest
{
    static void Main(string[] args)
    {
        TextToHTML example = new TextToHTML();
        example.Add("Hola");
        example.Add("uno dos");
        example.Add("tres cuatro");
        example.Display();
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.