Programming Course in C# ¡Free!

Text to HTML

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

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

Soy yo
Ya he terminado

should become


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.



using System;

class TextToHTML
private string[] html;
private int lines;
private int count;

public TextToHTML()
count = 0;
lines = 1000;

html = new string[lines];

public void Add(string line)
if (count < lines)
html[count] = line;

public string ToString()
string textHtml;

textHtml = "\n";
textHtml += "\n";

for (int i = 0; i < count; i++)
textHtml += "

"; textHtml += html[i]; textHtml += "

\n"; } textHtml += "\n"; textHtml += "\n"; return textHtml; } public void Display() { Console.Write( ToString() ); } } class Test { static void Main() { TextToHTML textToHTML = new TextToHTML(); textToHTML.Add("Hello"); textToHTML.Add("How are you?"); textToHTML.Display(); } }