Advertisement
dimipan80

Text Gravity

May 24th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. /* Write a program that takes as input a line length and text and formats the text so that it fits inside several rows, each with length equal to the given line length. Once the text is fitted, each character starts dropping as long as there is an empty space below it.
  2.  * Text characters start 'falling' until no whitespace remain under any character. The resulting text should be printed as an HTML table with each character in <td></td> tags.
  3.  * The output consists of the HTML table. Everything should be put inside <table></table> tags. Each line should be printed in <tr></tr> tags. Each character should be printed in <td></td> tags (encode the HTML special characters with the SecurityElement.Escape() method). Print space " " in all empty cells. */
  4.  
  5. namespace _14.Text_Gravity
  6. {
  7.     using System;
  8.     using System.Security;
  9.  
  10.     class TextGravity
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int lineLength = int.Parse(Console.ReadLine());
  15.             string text = Console.ReadLine();
  16.  
  17.             int rows = (int)Math.Ceiling(text.Length / (double)lineLength);
  18.             char[,] matrix = new char[rows, lineLength];
  19.             FillTheMatrix(matrix, text);
  20.  
  21.             DropingDownCharactersInMatrix(matrix);
  22.  
  23.             PrintMatrix(matrix);
  24.         }
  25.  
  26.         private static void FillTheMatrix(char[,] matrix, string text)
  27.         {
  28.             int indexLetter = 0;
  29.             for (int row = 0; row < matrix.GetLength(0); row++)
  30.             {
  31.                 for (int col = 0; col < matrix.GetLength(1); col++)
  32.                 {
  33.                     matrix[row, col] = ' ';
  34.                     if (indexLetter < text.Length)
  35.                     {
  36.                         matrix[row, col] = text[indexLetter];
  37.                         indexLetter++;
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.  
  43.         private static void DropingDownCharactersInMatrix(char[,] matrix)
  44.         {
  45.             for (int i = matrix.GetLength(0) - 1; i > 0; i--)
  46.             {
  47.                 for (int col = 0; col < matrix.GetLength(1); col++)
  48.                 {
  49.                     for (int row = matrix.GetLength(0) - 1; row > 0; row--)
  50.                     {
  51.                         if (matrix[row, col] == ' ')
  52.                         {
  53.                             matrix[row, col] = matrix[row - 1, col];
  54.                             matrix[row - 1, col] = ' ';
  55.                         }
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.  
  61.         private static void PrintMatrix(char[,] matrix)
  62.         {
  63.             Console.Write("<table>");
  64.             for (int row = 0; row < matrix.GetLength(0); row++)
  65.             {
  66.                 Console.Write("<tr>");
  67.                 for (int col = 0; col < matrix.GetLength(1); col++)
  68.                 {
  69.                     Console.Write("<td>{0}</td>",
  70.                         SecurityElement.Escape(matrix[row, col].ToString()));
  71.                 }
  72.  
  73.                 Console.Write("</tr>");
  74.             }
  75.  
  76.             Console.Write("</table>");
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement