Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exercise
- public static string smorse(string input)
- {
- string output = ""; // Creates a data structure to be used as a returned value
- //Takes the whole aphabet, stores it inside a string
- string morse_alphabet = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
- string[] morse = morse_alphabet.Split(' '); //Splits the whole aphabet into an array, because I like my life easier.
- //Goes through every letter in the alphabet, converts input to all lower-case so I don't get any problems.
- foreach (char i in input.ToLower())
- output +=morse[(int)i - 97]; // alphabet has 26 letters. Converting a letter to ascii gives a number.
- // 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
- return output; //Returns the value
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement