Advertisement
kanchev_k

GetLimitedString

Feb 3rd, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. //06. Write a program that reads from the console a string of maximum 20 characters.
  5. //If the length of the string is less than 20, the rest of the characters should
  6. //be filled with '*'. Print the result string into the console.
  7.  
  8. internal class GetLimitedString
  9. {
  10.     private static void Main()
  11.     {
  12.         int strLength = 20;
  13.         StringBuilder str = new StringBuilder("", strLength);
  14.         string userInput = "";
  15.         while (true)
  16.         {
  17.             Console.Write("\nEnter a string of maximum {0} characters: ", strLength);
  18.             userInput = Console.ReadLine();
  19.             if (userInput.Length <= strLength)
  20.             {
  21.                 break;
  22.             }
  23.             Console.WriteLine("Invalid input! Please try again...");
  24.         }
  25.         str.Append(userInput);
  26.         for (int i = userInput.Length; i < strLength; i++)
  27.         {
  28.             str.Append("*");
  29.         }
  30.         userInput = str.ToString();
  31.         Console.WriteLine(userInput);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement