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

Hello, I’m working on this task: President is even more weird

It’s look like code should work, but I can’t pass the validation.
Please, help me find what I did wrong.

Here is my code:


namespace Static
{

   static class TextFramePretifierTwo
   {
       private static int _textsPretified;
       static TextFramePretifierTwo()
       {
           _textsPretified = 0;
       }
       public static void Prettify(string text, int frameWidth)
       {
           string temp="";
           string[] strArray = text.Split(" ");
           _textsPretified++;
           Console.WriteLine($"Processing text: {_textsPretified}");
           
           for (int i = 0; i < frameWidth; i++)
           {
               Console.Write("*");
           }
           Console.WriteLine("");
           
           for (int j=0; j<strArray.Length-1; j++)
           {   
               if ((strArray[j].Length + temp.Length) < frameWidth - 4)
               {
                   temp =temp+strArray[j] + " ";
               } else 
               
               {
                   temp=temp.Trim().PadRight(frameWidth-4);
                   
                   Console.WriteLine($"* {temp} *");
                   temp = strArray[j]+" ";
               } 
               
           }
               
           if ((strArray[strArray.Length-1].Length + temp.Length) > frameWidth - 4)
           {
                   temp=temp.Trim().PadRight(frameWidth-4);
                   
                   Console.WriteLine($"* {temp} *");
                   Console.WriteLine($"* {strArray[strArray.Length-1].Trim().PadRight(frameWidth-4)} *");
                   
           } else {
                   temp =temp+strArray[strArray.Length-1] + " ";
                   temp=temp.Trim().PadRight(frameWidth-4);
                   Console.WriteLine($"* {temp} *");
               }
             

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

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

   class PresidentIsEvenMoreWeird
   {
       public static void Main()
       {
           int frameWidth = Convert.ToInt32(Console.ReadLine());
           string text = Console.ReadLine();
           while (text != "exit")
           {
               TextFramePretifierTwo.Prettify(text, frameWidth);
               text = Console.ReadLine();
           }
           // Write your code here    
       }
   }
}

Can anyone please help me to solve it?

In your solution you’re always adding a space after each world. Thus it will not “touch a frame”. However, while making a check in this line:

if ((strArray[j].Length + temp.Length) < frameWidth - 4)

you reserve an extra space and that’s why test fails.
Consider changing it to

if ((strArray[j].Length + temp.Length) < frameWidth - 3)

Thanks, it’s help. But I still not understand why my variant did not work. I used Trim() for remove all white-space characters:

Or it’s doesn’t work?

you use trim later, not on this comparison line, your solution always have 1 EXTRA space. You would easily see the difference while testing specifically with the failed test line:

23
You and I, my fellow citizens, need to be strong in our faith that all nations under God will reach the goal of peace with justice. May we be ever unswerving in devotion to principle, confident but humble with power, diligent in pursuit of the nations.
It is time for me to go
exit

Pay attention to “diligent in pursuit” part in your code and the fixed one :slight_smile: