Please help me with the task: President is even more weird

Hello, I’m working on this task:

The president was happy with your implementation of the TextFramePretifier… for one day. Then he decided that he wants to control the width of the frame. Take the code of the previous task and modify it to take an integer that represents the width of the frame and then print a message in the frame as you did in previous task, but using the given width this time. Write as many words as you can in each line; if there is an empty space, fill it with spaces. The number of words in a message will not exceed 100. All words are guaranteed to be shorter than (frameWidth - 4). Treat spaces as word separators and all other symbols, such as commas or question marks, as part of the word they’re connected to. The text is guaranteed to have only single spaces.
For example:
>15
>You look great today, sir!
Processing text: 1
***************
* You look *
* great *
* today, sir! *
***************
>A zebra does not change its spots.
Processing text: 2
***************
* A zebra *
* does not *
* change its *
* spots. *
***************
>exit

Here is my code:


namespace Static
{
   
   static class TextFramePretifier
   {
       private static int _textsPretified;

       static TextFramePretifier()
       {
           _textsPretified = 0;
       }

       public static void Prettify(string text, int length)
       {
           _textsPretified++;
           Console.WriteLine($"Processing text: {_textsPretified}");

           string[] strArray = text.Split(" ");
           int count = 0;

           for (int i = 0; i < length; i++)
           {
               Console.Write("*");
           }
           Console.WriteLine();
           Console.Write("*");
           for (int i = 0; i < strArray.Length; i++)
           {
               count += strArray[i].Length;

               if(count <= length - 4)
               {
                   
                   Console.Write($" {strArray[i]}");
                   count++;
               }
               else if(count > length - 4)
               {
                   count -= strArray[i].Length;
                   for (int j = count; j < length - 3; j++)
                       Console.Write(" ");

                   Console.Write(" *");
                   Console.WriteLine();
                   count = strArray[i].Length + 1;
                   Console.Write($"* {strArray[i]}");


               }
           }
           Console.Write(" *");
           Console.WriteLine();

           for (int i = 0; i < length; i++)
           {
               Console.Write("*");
           }
           Console.WriteLine();
       }
   }

   class PresidentIsEvenMoreWeird
   {
       public static void Main()
       {
           int length = int.Parse(Console.ReadLine());

           bool continueCycle = true;
           while(continueCycle)
           {
               var input = Console.ReadLine();
               if (input == "exit")
                   continueCycle = false;
               else
                   TextFramePretifier.Prettify(input, length);
           }
       }
   }
}

Can anyone please help me to solve it?

I’d say that’s rather a hack, than a tips, but on the last line you’re not printing enough of spaces and it looks weird. You could obviously add the following if inside your for (int i = 0; i < strArray.Length; i++):

               if(i == strArray.Length -1 ) {
                for(int k = count; k< length -3; k++)
                    Console.Write(" ");
               }

And a Console.WriteLine(); before exiting a loop, but have a look afterwards on the solution from Codeasy :wink:

1 Like

thank you very much, I watched the first incom so long time anddid not try other inputs that showed very well