Advertisement
yahorrr

Untitled

Sep 24th, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 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.  
  33.             while (endOfWord != -1)
  34.             {
  35.                 result += ToPigWord(phrase[position..endOfWord]);
  36.                 position = endOfWord;
  37.                 endOfWord = EndOfWord(phrase, position);
  38.             }
  39.  
  40.             return result;
  41.         }
  42.  
  43.         private static bool IsVowel(char symbol) => symbol is 'a' || symbol is 'A' ||
  44.                                                     symbol is 'o' || symbol is 'O' ||
  45.                                                     symbol is 'e' || symbol is 'E' ||
  46.                                                     symbol is 'i' || symbol is 'I' ||
  47.                                                     symbol is 'u' || symbol is 'U';
  48.  
  49.         private static string ToPigWord(string phrase)
  50.         {
  51.             if (IsVowel(phrase[0]))
  52.             {
  53.                 return phrase + "yay";
  54.             }
  55.  
  56.             int position = VowelFirstPosition(phrase);
  57.  
  58.             return char.IsUpper(phrase[0]) ? char.ToUpper(phrase[position], CultureInfo.InvariantCulture) + phrase[(position + 1)..] + char.ToLower(phrase[0], CultureInfo.InvariantCulture) + phrase[1..position] + "ay" : phrase[position..] + phrase[..position] + "ay";
  59.         }
  60.  
  61.         private static int VowelFirstPosition(string phrase, int position = 0)
  62.         {
  63.             for (int i = position; i < phrase.Length; i++)
  64.             {
  65.                 if (IsVowel(phrase[i]))
  66.                 {
  67.                     return i;
  68.                 }
  69.             }
  70.  
  71.             return -1;
  72.         }
  73.  
  74.         private static int EndOfWord(string phrase, int position = 0)
  75.         {
  76.             for (int i = position; i < phrase.Length; i++)
  77.             {
  78.                 if (!char.IsLetter(phrase[i]))
  79.                 {
  80.                     return i;
  81.                 }
  82.             }
  83.  
  84.             return -1;
  85.         }
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement