Advertisement
stefanMinch3v

SoftUni/String&RegexExercise/8.LettersChangeNumbers

Mar 3rd, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 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.  
  7.  
  8. public class LettersChangeNumbers
  9. {
  10.     public static void Main(string[] args)
  11.     {
  12.         string[] input = Console.ReadLine().Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
  13.         char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ';' }; //add this ";" because the sequence starts from 0 and then on each row just add + 1
  14.         char[] alphabetUpper = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ';' }; //add this ";" because the sequence starts from 0 and then on each row just add + 1
  15.         List<decimal> wordResults = new List<decimal>();
  16.  
  17.         //main loop
  18.         for (int i = 0; i < input.Length; i++)
  19.         {
  20.             string word = null; //reset the word
  21.             word = input[i];
  22.             string saveStringDigit = null; //save the digit as string
  23.             decimal convertStringToDecimal = 0; // convert from string to decimal
  24.             int indexOfAlphabetUpper = 0; // save the index of the letter in alphabet upper
  25.             int indexOfAlphabetLower = 0; // save the index of the letter in alphabet lower
  26.             int indexFirst = 0; // keep the first index of the word
  27.             int indexLast = word.Length - 1; // keep the last index of the word
  28.  
  29.             //converting char to string , after that to decimal
  30.             for (int j = 0; j < word.Length; j++)
  31.             {
  32.                 if (char.IsDigit(word[j]))
  33.                 {
  34.                     string saveDigit = word[j].ToString();
  35.                     saveStringDigit += saveDigit;
  36.                 }
  37.             }
  38.             convertStringToDecimal = decimal.Parse(saveStringDigit);
  39.  
  40.             //check first index
  41.             if (char.IsUpper(word[indexFirst]))
  42.             {
  43.                 for (int j = 0; j < alphabetUpper.Length; j++)
  44.                 {
  45.                     if (word[indexFirst].Equals(alphabetUpper[j]))
  46.                     {
  47.                         indexOfAlphabetUpper = Array.IndexOf(alphabetUpper, alphabetUpper[j + 1]);
  48.                         convertStringToDecimal /= indexOfAlphabetUpper;
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             //check first index
  54.             if (char.IsLower(word[indexFirst]))
  55.             {
  56.                 for (int j = 0; j < alphabet.Length; j++)
  57.                 {
  58.                     if (word[indexFirst].Equals(alphabet[j]))
  59.                     {
  60.                         indexOfAlphabetLower = Array.IndexOf(alphabet, alphabet[j + 1]);
  61.                         convertStringToDecimal *= indexOfAlphabetLower;
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             //check last index
  67.             if (char.IsUpper(word[indexLast]))
  68.                 {
  69.                     for (int j = 0; j < alphabetUpper.Length; j++)
  70.                     {
  71.                         if (word[indexLast].Equals(alphabetUpper[j]))
  72.                         {
  73.                             indexOfAlphabetUpper = Array.IndexOf(alphabetUpper, alphabetUpper[j + 1]);
  74.                             convertStringToDecimal -= indexOfAlphabetUpper;
  75.                         }
  76.                     }
  77.                 }
  78.  
  79.             //check last index
  80.             if (char.IsLower(word[indexLast]))
  81.             {
  82.                 for (int j = 0; j < alphabet.Length; j++)
  83.                 {
  84.                     if (word[indexLast].Equals(alphabet[j]))
  85.                     {
  86.                         indexOfAlphabetLower = Array.IndexOf(alphabet, alphabet[j + 1]);
  87.                         convertStringToDecimal += indexOfAlphabetLower;
  88.                     }
  89.                 }
  90.             }
  91.            
  92.             //add the result in the list
  93.             wordResults.Add(convertStringToDecimal);
  94.         }
  95.  
  96.         //linq sum each value in the list and finally print it out
  97.         decimal total = wordResults.Sum(x => x);
  98.         Console.WriteLine($"{total:f2}");
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement