Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- class ConsoleJustification
- {
- static void Main()
- {
- int lines = int.Parse(Console.ReadLine());
- int width = int.Parse(Console.ReadLine());
- StringBuilder text = new StringBuilder();
- for (int i = 0; i < lines; i++)
- {
- text.Append(Console.ReadLine());
- text.Append(' ');
- }
- string strText = text.ToString();
- List<string> words = new List<string>();
- char[] separators = { ' ', ' ' };
- words.AddRange(strText.Split(separators,StringSplitOptions.RemoveEmptyEntries));
- Printing(words, width);
- }
- static void Printing (List<string> words, int width)
- {
- StringBuilder printLine = new StringBuilder();
- while (words.Count > 0)
- {
- int wordsOnRow = 0;
- if (printLine.Length == 0)
- {
- printLine.Append(words[0]);
- words.RemoveAt(0);
- wordsOnRow++;
- }
- while (words.Count>0 && (width - (printLine.Length + 1) >= words[0].Length))
- {
- printLine.Append(' ');
- printLine.Append(words[0]);
- words.RemoveAt(0);
- wordsOnRow++;
- }
- Console.WriteLine(Justification(printLine,width,wordsOnRow));
- printLine.Clear();
- }
- }
- static string Justification(StringBuilder printLine, int width, int wordsOnRow)
- {
- int position = 1;
- while (printLine.Length < width && wordsOnRow != 1)
- {
- if (printLine[position] == ' ' && printLine[position-1] != ' ')
- {
- printLine.Insert(position,' ');
- }
- if (position == printLine.Length-1) position = 1;
- position++;
- }
- return printLine.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement