Advertisement
Guest User

TextBombardment

a guest
Dec 21st, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class TextBombardment
  5. {
  6.     static void Main()
  7.     {
  8.         //parse input
  9.         string text = Console.ReadLine();
  10.         int lineWidth = int.Parse(Console.ReadLine());
  11.         string columnsToBomb = Console.ReadLine();
  12.  
  13.         //calculate the number of rows needed to fit the text
  14.         decimal textLength = text.Length;
  15.         int rows = (int)Math.Ceiling(textLength / lineWidth);
  16.  
  17.         //initialise an array of characters
  18.         char[,] table = new char[rows, lineWidth];
  19.  
  20.         //fill it with the letters of the text
  21.         int index = 0;
  22.         for (int row = 0; row < table.GetLength(0); row++)
  23.         {
  24.             for (int col = 0; col < table.GetLength(1); col++)
  25.             {
  26.                 table[row, col] = text[index];
  27.                 index++;
  28.                 if (index > (textLength - 1))
  29.                 {
  30.                     break;
  31.                 }
  32.             }
  33.         }
  34.         ////check the letters in the table
  35.         //for (int row = 0; row < table.GetLength(0); row++)
  36.         //{
  37.         //    for (int col = 0; col < table.GetLength(1); col++)
  38.         //    {
  39.         //        Console.Write("{0} ", table[row, col]);
  40.         //    }
  41.         //    Console.WriteLine();
  42.         //}
  43.  
  44.         //split the command to bomb columns to an array
  45.         int[] bomb = SplitString(columnsToBomb);
  46.  
  47.         //go through the array column by column
  48.         for (int col = 0; col < table.GetLength(1); col++)
  49.         {
  50.             for (int i = 0; i < bomb.Length; i++)
  51.             {
  52.                 if (bomb[i] == col)
  53.                 {
  54.                     //check if the bomb has met a character (not space) and has exploded
  55.                     bool bombExploded = false;
  56.  
  57.                     //go through each column from top to bottom
  58.                     for (int row = 0; row < table.GetLength(0); row++)
  59.                     {
  60.                         //the ASCII code for SPACE is 32
  61.                         if (table[row, col] != 32)
  62.                         {
  63.                             bombExploded = true;
  64.                         }
  65.                         //if the bomb has exploded and the next cell
  66.                         //is a space, leave the inner loop
  67.                         //we MUST NOT bomb the letters below
  68.                         if (bombExploded == true && table[row, col] == 32)
  69.                         {
  70.                             break;
  71.                         }
  72.  
  73.                         //set the cell to a SPACE ASCII symbol
  74.                         table[row, col] = (char)32;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.         //assemble the final result
  80.         StringBuilder build = new StringBuilder();
  81.         for (int row = 0; row < table.GetLength(0); row++)
  82.         {
  83.             for (int col = 0; col < table.GetLength(1); col++)
  84.             {
  85.                 //DO NOT add NULL string values at the end!
  86.                 if (table[row, col] != '\0')
  87.                 {
  88.                     build.Append(table[row, col]);
  89.                 }
  90.             }
  91.         }
  92.         string result = build.ToString();
  93.  
  94.         //print the final result!!!
  95.         Console.WriteLine(result);
  96.     }
  97.     public static int[] SplitString(string input)
  98.     {
  99.         string[] array = input.Split(' ');
  100.         int[] numbers = new int[array.Length];
  101.         for (int i = 0; i < numbers.Length; i++)
  102.         {
  103.             numbers[i] = int.Parse(array[i]);
  104.         }
  105.         return numbers;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement