Advertisement
foolkiller204

Display.WriteLine()

Oct 12th, 2023
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | Source Code | 0 0
  1. class Display
  2.         {
  3.             //gets user input and max number of characters the user wants on each line
  4.             public static void Write(string input,int maxChar = 50)
  5.             {
  6.                 //variables
  7.                 string[] string_array = input.Split(" ");
  8.                 int number_of_char = 0;
  9.                 int i = 0;
  10.                 //runs through each word in the big string
  11.                 foreach (string str in string_array)
  12.                 {
  13.                     //acounts for every character in each word including spaces
  14.                     number_of_char += str.Length + 1;
  15.                     //if the number of characters exceedes max character limit a new line is created
  16.                     if (number_of_char >= maxChar)
  17.                     {
  18.                         if (i<string_array.Length)
  19.                         {
  20.                             Console.Write($"\n{str} ");
  21.                             //resets char count for new line
  22.                             number_of_char = 0;
  23.                         }
  24.                     }
  25.                     //continues printing normaly if the character limit for that line is not reached
  26.                     else
  27.                     {
  28.                         Console.Write(str+" ");
  29.                     }
  30.                     i++;
  31.                 }
  32.             }
  33.             public static void WriteLine(string input, int maxChar = 50)
  34.             {
  35.                 string[] string_array = input.Split(" ");
  36.                 int number_of_char = 0;
  37.                 foreach (string str in string_array)
  38.                 {
  39.                     number_of_char += str.Length + 1;
  40.                     if (number_of_char >= maxChar)
  41.                     {
  42.                         Console.Write($"\n{str} ");
  43.                         number_of_char = 0;
  44.                     }
  45.                     else
  46.                     {
  47.                         Console.Write(str + " ");
  48.                     }
  49.  
  50.                 }
  51.                 Console.WriteLine();
  52.             }
  53.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement