Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Text;
- namespace LanguageGame
- {
- public static class Translator
- {
- /// <summary>
- /// Translates from English to Pig Latin. Pig Latin obeys a few simple following rules:
- /// - if word starts with vowel sounds, the vowel is left alone, and most commonly 'yay' is added to the end;
- /// - if word starts with consonant sounds or consonant clusters, all letters before the initial vowel are
- /// placed at the end of the word sequence. Then, "ay" is added.
- /// Note: If a word begins with a capital letter, then its translation also begins with a capital letter,
- /// if it starts with a lowercase letter, then its translation will also begin with a lowercase letter.
- /// </summary>
- /// <param name="phrase">Source phrase.</param>
- /// <returns>Phrase in Pig Latin.</returns>
- /// <exception cref="ArgumentException">Thrown if phrase is null or empty.</exception>
- /// <example>
- /// "apple" -> "appleyay"
- /// "Eat" -> "Eatyay"
- /// "explain" -> "explainyay"
- /// "Smile" -> "Ilesmay"
- /// "Glove" -> "Oveglay"
- /// </example>
- public static string TranslateToPigLatin(string phrase)
- {
- if (string.IsNullOrWhiteSpace(phrase))
- {
- throw new ArgumentException("given phrase is null or empty", nameof(phrase));
- }
- char[] alphabet = "abcdefghijklmnopqrstuvwxyz’ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
- char[] vowels = "aeiouAEIOU".ToCharArray();
- int found = phrase.IndexOfAny(alphabet);
- string result = phrase;
- while (found != -1)
- {
- int endOfWord = IndexNotOf(result, alphabet, found);
- int vowelIndex = result.IndexOfAny(vowels, found);
- if (found == vowelIndex)
- {
- result = result.Insert(endOfWord, "yay");
- found = result.IndexOfAny(alphabet, endOfWord + 3);
- continue;
- }
- if (endOfWord < vowelIndex)
- {
- result = result.Insert(endOfWord, "ay");
- found = result.IndexOfAny(alphabet, endOfWord + 2);
- continue;
- }
- StringBuilder builder = new StringBuilder(result);
- builder.Remove(found, endOfWord - found);
- builder.Insert(found, result[vowelIndex..endOfWord] + result[found..vowelIndex] + "ay");
- if (char.IsUpper(result[found]))
- {
- builder[found] = char.ToUpper(builder[found], CultureInfo.InvariantCulture);
- builder[endOfWord - vowelIndex + found] = char.ToLower(builder[endOfWord - vowelIndex + found], CultureInfo.InvariantCulture);
- }
- result = builder.ToString();
- found = result.IndexOfAny(alphabet, endOfWord + 2);
- }
- return result;
- }
- private static int IndexNotOf(string source, char[] array, int startIndex)
- {
- bool notFound = false;
- for (int i = startIndex; i < source.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- if (source[i] == array[j])
- {
- notFound = false;
- break;
- }
- notFound = true;
- }
- if (notFound)
- {
- return i;
- }
- }
- return source.Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement