Advertisement
Normantas

r/DailyProgramming Challenge #380 [Easy] Solution

Dec 17th, 2019
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. //Exercise
  2.  
  3. public static string smorse(string input)
  4.         {
  5.             string output = ""; // Creates a data structure to be used as a returned value
  6.  
  7.             //Takes the whole aphabet, stores it inside a string
  8.             string morse_alphabet = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
  9.             string[] morse = morse_alphabet.Split(' '); //Splits the whole aphabet into an array, because I like my life easier.
  10.             //Goes through every letter in the alphabet, converts input to all lower-case so I don't get any  problems.
  11.             foreach (char i in input.ToLower())
  12.                 output +=morse[(int)i - 97]; // alphabet has 26 letters. Converting a letter to ascii gives a number.
  13.            
  14.             // NOTE: EXPLANATION     'a' is 97 and 'z' is 122 (range: 0 - 25), so if you take a lower-case letter and minus '97' of it, you will get it's position in morse array; I use explicit type casting to get a value of a char in ASCII
  15.  
  16.             return output; //Returns the value
  17.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement