Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4.  
  5. namespace Assignment
  6. {
  7. public struct BoldText
  8. {
  9. public int indexFirst;
  10. public int indexLast;
  11. }
  12.  
  13. public class Program
  14. {
  15. public static void Main(string[] args)
  16. {
  17. List<BoldText> bold = new List<BoldText>();
  18.  
  19. Console.WriteLine("Type the text You want to change");
  20. string text = Console.ReadLine();
  21.  
  22. int inputEnd = 1;
  23.  
  24. while(inputEnd != 0)
  25. {
  26. Console.WriteLine("Declare the beginning of the bolded text");
  27. int first = Convert.ToInt32(Console.ReadLine());
  28.  
  29. Console.WriteLine("Declare the ending of the bolded text");
  30. int last = Convert.ToInt32(Console.ReadLine());
  31.  
  32. bold.Add( new BoldText {indexFirst = first, indexLast = last} );
  33.  
  34. Console.WriteLine("If You want to stop declaring, type 0, otherwise type any other number");
  35. inputEnd = Convert.ToInt32(Console.ReadLine());
  36. }
  37. changeToHtml(text, bold);
  38. }
  39.  
  40. public static void changeToHtml(string text, List<BoldText> bold)
  41. {
  42. int textLength = text.Length;
  43. bool[] isCharTagged = new bool[textLength];
  44.  
  45. foreach(BoldText bText in bold)
  46. {
  47. if((bText.indexFirst < textLength) && (bText.indexFirst+bText.indexLast < textLength)) // Verification if indexes do not exceed the length.
  48. {
  49. var changedText = new StringBuilder(); // A new string, where the new text will be written.
  50. int letter = 0; // Sort of a index of the text (excluding the HTML tag).
  51. char prev = ' '; // A variable for the "previous" character.
  52.  
  53. foreach (char curr in text)
  54. {
  55. if((letter == 0) && (letter == bText.indexFirst))
  56. {
  57. changedText.Append("<b>"); // The HTML tag for the begging of the bold text is added.
  58.  
  59. if (letter == (bText.indexFirst+bText.indexLast))
  60. {
  61. changedText.Append("</b>"); // The HTML tag for the ending of the bold text is added.
  62. }
  63. }
  64.  
  65. changedText.Append(curr); // Adding the character to the string.
  66.  
  67. if((!isTag(prev, curr)) && (!isCharTagged[bText.indexFirst]) && (!isCharTagged[bText.indexFirst+bText.indexLast]))
  68. {
  69. if (++letter == bText.indexFirst) // If the number of the letter equals the first index.
  70. {
  71. changedText.Append("<b>"); // The HTML tag for the begging of the bold text is added.
  72. }
  73. if (letter == (bText.indexFirst+bText.indexLast)) // If the number of the letter equals the last index.
  74. {
  75. changedText.Append("</b>"); // The HTML tag for the ending of the bold text is added.
  76. }
  77. }
  78. prev = curr; // Saving the character (it becomes the "previous" one).
  79. }
  80.  
  81. for (int j = bText.indexFirst; j < (bText.indexFirst+bText.indexLast); j++) // The text between the indexes is tagged.
  82. {
  83. isCharTagged[j] = true;
  84. }
  85.  
  86. text = changedText.ToString();
  87. }
  88. }
  89. Console.WriteLine(text);
  90. }
  91.  
  92. public static bool isTag(char prev, char curr)
  93. {
  94. if ((curr == '<') || (curr == '/') || ((curr == 'b') && ((prev == '<') || (prev == '/'))) || (curr == '>'))
  95. return true;
  96. else
  97. return false;
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement