Advertisement
grubcho

Average char delimiter

Jul 3rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will receive an array of strings as input. Your task is to find the average character within every string in the array. As in, //take every character’s ASCII code, sum it and divide the result by the sum of the count of all the letters in the array.
  7. //Example:
  8. //a b ab abc abc  a, b, a, b a, b, c, a, b, c  sum: 978  Divide it by 10: 97.8  97
  9. //After you find the average sum, convert it to its ASCII character representation, convert it to uppercase and print the original //array, delimited by that character.
  10.  
  11. namespace char_delimiter
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string[] arr = Console.ReadLine().Split(' ').ToArray();
  18.             int sumAllElements = 0;
  19.             int countAllElements = 0;
  20.             var delimiter = string.Empty;
  21.  
  22.             for (int i = 0; i < arr.Length; i++)
  23.             {
  24.                 string currentElement = arr[i];
  25.                 for (int j = 0; j < currentElement.Length; j++)
  26.                 {
  27.                     int letter = currentElement[j];
  28.                     countAllElements ++;
  29.                     sumAllElements += letter;
  30.                 }
  31.             }
  32.             int avgSum = sumAllElements / countAllElements;
  33.             if (avgSum >= 97 && avgSum <= 122)
  34.             {
  35.                 avgSum -= 32;
  36.             }                
  37.             char avgChar = (char)avgSum;
  38.             delimiter += avgChar;
  39.             Console.WriteLine(string.Join(delimiter, arr));
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement