Advertisement
yahorrr

Untitled

Sep 25th, 2022
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System;
  2. using System.Buffers;
  3. using System.Globalization;
  4.  
  5. namespace LanguageGame
  6. {
  7.     public static class Translator
  8.     {
  9.         /// <summary>
  10.         /// Translates from English to Pig Latin. Pig Latin obeys a few simple following rules:
  11.         /// - if word starts with vowel sounds, the vowel is left alone, and most commonly 'yay' is added to the end;
  12.         /// - if word starts with consonant sounds or consonant clusters, all letters before the initial vowel are
  13.         ///   placed at the end of the word sequence. Then, "ay" is added.
  14.         /// Note: If a word begins with a capital letter, then its translation also begins with a capital letter,
  15.         /// if it starts with a lowercase letter, then its translation will also begin with a lowercase letter.
  16.         /// </summary>
  17.         /// <param name="phrase">Source phrase.</param>
  18.         /// <returns>Phrase in Pig Latin.</returns>
  19.         /// <exception cref="ArgumentException">Thrown if phrase is null or empty.</exception>
  20.         /// <example>
  21.         /// "apple" -> "appleyay"
  22.         /// "Eat" -> "Eatyay"
  23.         /// "explain" -> "explainyay"
  24.         /// "Smile" -> "Ilesmay"
  25.         /// "Glove" -> "Oveglay"
  26.         /// </example>
  27.         public static string TranslateToPigLatin(string phrase)
  28.         {
  29.             int position = 0;
  30.             int endOfWord = EndOfWord(phrase, position);
  31.             string result = string.Empty;
  32.             do
  33.             {
  34.                 result += ToPigWord(phrase[position..endOfWord]);
  35.                 position = IndexOfLetter(phrase, endOfWord);
  36.                 result += phrase[endOfWord..position];
  37.                 endOfWord = EndOfWord(phrase, position);
  38.             }
  39.             while (position != -1 || endOfWord != -1);
  40.            
  41.             return result;
  42.         }
  43.  
  44.         private static bool IsVowel(char symbol) => symbol is 'a' || symbol is 'A' ||
  45.                                                     symbol is 'o' || symbol is 'O' ||
  46.                                                     symbol is 'e' || symbol is 'E' ||
  47.                                                     symbol is 'i' || symbol is 'I' ||
  48.                                                     symbol is 'u' || symbol is 'U';
  49.  
  50.         private static string ToPigWord(string phrase)
  51.         {
  52.             if (IsVowel(phrase[0]))
  53.             {
  54.                 return phrase + "yay";
  55.             }
  56.  
  57.             int position = VowelFirstPosition(phrase);
  58.  
  59.             return char.IsUpper(phrase[0])
  60.                 ? char.ToUpper(phrase[position], CultureInfo.InvariantCulture) + phrase[(position + 1) ..] +
  61.                   char.ToLower(phrase[0], CultureInfo.InvariantCulture) + phrase[1..position] + "ay"
  62.                 : phrase[position..] + phrase[..position] + "ay";
  63.         }
  64.  
  65.         private static int VowelFirstPosition(string phrase, int position = 0)
  66.         {
  67.             for (int i = position; i < phrase.Length; i++)
  68.             {
  69.                 if (IsVowel(phrase[i]))
  70.                 {
  71.                     return i;
  72.                 }
  73.             }
  74.  
  75.             return -1;
  76.         }
  77.  
  78.         private static int EndOfWord(string phrase, int position = 0)
  79.         {
  80.             for (int i = position; i < phrase.Length; i++)
  81.             {
  82.                 if (!char.IsLetter(phrase[i]))
  83.                 {
  84.                     return i;
  85.                 }
  86.             }
  87.  
  88.             return -1;
  89.         }
  90.  
  91.         private static int IndexOfLetter(string phrase, int position = 0)
  92.         {
  93.             for (int i = position; i < phrase.Length; i++)
  94.             {
  95.                 if (char.IsLetter(phrase[i]))
  96.                 {
  97.                     return i;
  98.                 }
  99.             }
  100.  
  101.             return -1;
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement