Advertisement
Guest User

Untitled

a guest
Nov 10th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace loopExr_10_partyInvitation
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             // declare variables
  15.             string name = "";
  16.             int allNames = 0;
  17.             int invited = 0;
  18.  
  19.             while (true)
  20.             {
  21.                 name = Console.ReadLine();
  22.  
  23.                 if (name == "Statistic") break;
  24.                 if (name == "") Console.WriteLine("Invalid name!");
  25.                 int letters = 0;
  26.  
  27.                 // loop each letter
  28.                 for (int i = 0; i < name.Length; i++)
  29.                 {
  30.                     int n = (int)name[i];
  31.  
  32.                     // check if it is a latin letter
  33.                     if ((n >= 65 && n <= 90) || (n >= 97 && n <= 122))
  34.                     {
  35.                         letters++;
  36.                     }
  37.                 }
  38.                 // Console.WriteLine($"all - {name.Length}, valid = {letters}");
  39.                 // if all letters are here so it is valid name
  40.                 if (letters == name.Length)
  41.                 {
  42.                     TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
  43.                     Console.WriteLine(myTI.ToTitleCase(name));
  44.                     invited++;
  45.                 }
  46.                 else
  47.                 {
  48.                     Console.WriteLine("Invalid name!");
  49.                 }
  50.  
  51.                 // count all names
  52.                 allNames++;
  53.             }
  54.  
  55.             // print the statistic
  56.             Console.WriteLine($"Valid names are {100.00 * invited / allNames:F2}% from {allNames} names.");
  57.             Console.WriteLine($"Invalid names are {100.00 * (allNames - invited) / allNames:F2}% from {allNames} names.");
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement