Advertisement
yahorrr

Untitled

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