Advertisement
CuST0M1z3

ConsoleJustify

Jul 26th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. // За обяснения по source code посетете: http://yordanbonev.wordpress.com/
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9.  
  10. class ConsoleJustifyMethod
  11. {  
  12.     static void Main()
  13.     {
  14.         int numberOfLines = int.Parse(Console.ReadLine());
  15.         int numberOfSymbols = int.Parse(Console.ReadLine());
  16.  
  17.         StringBuilder text = new StringBuilder();
  18.  
  19.         for (int i = 0; i < numberOfLines; i++)
  20.         {
  21.             text.Append(Console.ReadLine());
  22.             text.Append(" ");
  23.         }
  24.         char[] separators = new char[] { ' ' };
  25.         string[] splittedText = text.ToString().Split(separators, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27.         OutputJustifyText(splittedText, numberOfSymbols);              
  28.     }
  29.     static void OutputJustifyText(string[] splittedText, int numberOfSymbols)
  30.     {
  31.  
  32.         StringBuilder outputText = new StringBuilder();
  33.  
  34.         int spaceCounter = 2;
  35.  
  36.         for (int i = 0; i < splittedText.Length; i++)
  37.         {
  38.             if (((outputText.ToString() + splittedText[i]).Length <= numberOfSymbols))
  39.             {
  40.                 outputText.Append(splittedText[i]);
  41.                 if ((outputText.ToString() + " ").Length < numberOfSymbols)
  42.                 {
  43.                     outputText.Append(" ");
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 AddOrRemoveSpaces(outputText, numberOfSymbols, spaceCounter);
  49.                 outputText.Append(splittedText[i] + " ");
  50.             }
  51.         }
  52.         AddOrRemoveSpaces(outputText, numberOfSymbols, spaceCounter);
  53.         return;
  54.     }
  55.  
  56.     static void AddOrRemoveSpaces(StringBuilder outputText, int numberOfSymbols, int spaceCounter)
  57.     {
  58.         if (outputText[outputText.Length - 1] == ' ')
  59.         {
  60.             outputText.Remove(outputText.Length - 1, 1);
  61.         }
  62.         int index = outputText.ToString().IndexOf(' ', 0);
  63.  
  64.         if (index == -1)
  65.         {
  66.             Console.WriteLine(outputText.ToString().TrimEnd(' '));
  67.             outputText.Clear();
  68.         }
  69.  
  70.         int firstIndex = index;
  71.         while (index != -1)
  72.         {
  73.             if (outputText.Length == numberOfSymbols)
  74.             {
  75.                 Console.WriteLine(outputText.ToString().TrimEnd(' '));
  76.                 outputText.Clear();
  77.                 spaceCounter = 2;
  78.                 break;
  79.             }
  80.             else
  81.             {
  82.                 outputText.Insert(index, ' ');
  83.                 index = outputText.ToString().IndexOf(' ', index + spaceCounter);
  84.             }
  85.             if (index == -1)
  86.             {
  87.                 spaceCounter++;
  88.                 index = firstIndex;
  89.             }
  90.         }
  91.         return;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement