Advertisement
tutzy

letters, symbols, numbers

Apr 2nd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. You are given N strings. Every string consists of letters, symbols, numbers and whitespace. All letters (a-z) and (A-Z) have values corresponding to their position in the English alphabet * 10 (a = 10, A = 10, b = 20, B = 20, …, z = 260, Z = 260). All symbols (like `~!@#$%^&*()_+{}:"|<>?-=[];'\,./) have a fixed value of 200 and all numbers have the value of their digit * 20 (0 = 0, 1 = 20, 2 = 40, 3 = 60, …, 9 = 180). The whitespace is ignored. Your task is to calculate the sum of all letters, all symbols and all numbers from the input and print them, each at a separate line.
  2. Input
  3. The input data should be read from the console.
  4. • At the first line an integer number N is given, specifying the count of the input strings.
  5. • Each of the next N lines holds an input string.
  6. The input data will always be valid and in the format described. There is no need to check it explicitly.
  7. Output
  8. The output should be printed on the console. It should consist of exactly 3 lines:
  9. • On the first line print the sum of all letters.
  10. • On the second line print the sum of all numbers.
  11. • On the third line print the sum of all symbols.
  12.  
  13.  
  14. using System;
  15. using System.Text.RegularExpressions;
  16. class Program
  17. {
  18.     static void Main()
  19.     {
  20.         int n = int.Parse(Console.ReadLine());
  21.         long lettersSum = 0;
  22.         long symbolsSum = 0;
  23.         long numbersSum = 0;
  24.         for (int i = 0; i < n; i++)
  25.         {
  26.             string line = Console.ReadLine();
  27.             line = line.ToUpper();
  28.             line = Regex.Replace(line, "\\s+", "");
  29.             for (int k = 0; k < line.Length; k++)
  30.             {
  31.                 char currentCar = line[k];
  32.                 if (currentCar >= 'A' && currentCar <= 'Z')
  33.                 {
  34.                     lettersSum += (currentCar - 'A' + 1) * 10;
  35.                 }
  36.                 else if (currentCar >= '0' && currentCar <= '9')
  37.                 {
  38.                     numbersSum += (currentCar - '0') * 20;
  39.                 }
  40.                 else
  41.                 {
  42.                    
  43.                     symbolsSum += 200;
  44.                 }
  45.             }
  46.         }
  47.         Console.WriteLine(lettersSum);
  48.         Console.WriteLine(numbersSum);
  49.         Console.WriteLine(symbolsSum);
  50.  
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement