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
C# Exercise Example
// 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());
}
}