Advertisement
yahorrr

Untitled

Sep 25th, 2022
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 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 = EndOfWord(phrase, position); ;
  31.             string result = string.Empty;
  32.  
  33.             while (position != -1 || endOfWord != -1)
  34.             {
  35.                 result += ToPigWord(phrase[position..endOfWord]);
  36.  
  37.                 if (IndexOfLetter(phrase, endOfWord) == -1)
  38.                 {
  39.                     position = phrase.Length;
  40.                 }
  41.                 else
  42.                 {
  43.                     position = IndexOfLetter(phrase, endOfWord);
  44.  
  45.                 }
  46.  
  47.                 result += phrase[endOfWord..position];
  48.                 endOfWord = EndOfWord(phrase, position);
  49.             }
  50.            
  51.             return result;
  52.         }
  53.  
  54.         private static bool IsVowel(char symbol) => symbol is 'a' || symbol is 'A' ||
  55.                                                     symbol is 'o' || symbol is 'O' ||
  56.                                                     symbol is 'e' || symbol is 'E' ||
  57.                                                     symbol is 'i' || symbol is 'I' ||
  58.                                                     symbol is 'u' || symbol is 'U';
  59.  
  60.         private static string ToPigWord(string phrase)
  61.         {
  62.             if (IsVowel(phrase[0]))
  63.             {
  64.                 return phrase + "yay";
  65.             }
  66.  
  67.             int position = VowelFirstPosition(phrase);
  68.             for (int i = 0; i < phrase.Length; i++)
  69.             {
  70.                 if (IsVowel(phrase[i]))
  71.                 {
  72.                     return char.IsUpper(phrase[0])
  73.                         ? char.ToUpper(phrase[position], CultureInfo.InvariantCulture) + phrase[(position + 1)..] +
  74.                           char.ToLower(phrase[0], CultureInfo.InvariantCulture) + phrase[1..position] + "ay"
  75.                         : phrase[position..] + phrase[..position] + "ay";
  76.                 }
  77.             }
  78.  
  79.             return phrase + "ay";
  80.         }
  81.  
  82.         private static int VowelFirstPosition(string phrase, int position = 0)
  83.         {
  84.             for (int i = position; i < phrase.Length; i++)
  85.             {
  86.                 if (IsVowel(phrase[i]))
  87.                 {
  88.                     return i;
  89.                 }
  90.             }
  91.  
  92.             return -1;
  93.         }
  94.  
  95.         private static int EndOfWord(string phrase, int position = 0)
  96.         {
  97.             for (int i = position; i < phrase.Length; i++)
  98.             {
  99.                 if (!char.IsLetter(phrase[i]))
  100.                 {
  101.                     return i;
  102.                 }
  103.             }
  104.  
  105.             return -1;
  106.         }
  107.  
  108.         private static int IndexOfLetter(string phrase, int position = 0)
  109.         {
  110.             for (int i = position; i < phrase.Length; i++)
  111.             {
  112.                 if (char.IsLetter(phrase[i]))
  113.                 {
  114.                     return i;
  115.                 }
  116.             }
  117.  
  118.             return -1;
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement