Advertisement
yahorrr

Untitled

Sep 23rd, 2022
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  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.             if (string.IsNullOrWhiteSpace(phrase))
  30.             {
  31.                 throw new ArgumentException("given phrase is null or empty", nameof(phrase));
  32.             }
  33.  
  34.             char[] alphabet = "abcdefghijklmnopqrstuvwxyz’ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
  35.             char[] vowels = "aeiouAEIOU".ToCharArray();
  36.             int found = phrase.IndexOfAny(alphabet);
  37.             string result = phrase;
  38.             while (found != -1)
  39.             {
  40.                 int endOfWord = IndexNotOf(result, alphabet, found);
  41.                 int vowelIndex = result.IndexOfAny(vowels, found);
  42.                 if (found == vowelIndex)
  43.                 {
  44.                     result = result.Insert(endOfWord, "yay");
  45.                     found = result.IndexOfAny(alphabet, endOfWord + 3);
  46.                     continue;
  47.                 }
  48.  
  49.                 if (endOfWord < vowelIndex)
  50.                 {
  51.                     result = result.Insert(endOfWord, "ay");
  52.                     found = result.IndexOfAny(alphabet, endOfWord + 2);
  53.                     continue;
  54.                 }
  55.  
  56.                 StringBuilder builder = new StringBuilder(result);
  57.                 builder.Remove(found, endOfWord - found);
  58.                 builder.Insert(found, result[vowelIndex..endOfWord] + result[found..vowelIndex] + "ay");
  59.                 if (char.IsUpper(result[found]))
  60.                 {
  61.                     builder[found] = char.ToUpper(builder[found], CultureInfo.InvariantCulture);
  62.                     builder[endOfWord - vowelIndex + found] = char.ToLower(builder[endOfWord - vowelIndex + found], CultureInfo.InvariantCulture);
  63.                 }
  64.  
  65.                 result = builder.ToString();
  66.                 found = result.IndexOfAny(alphabet, endOfWord + 2);
  67.             }
  68.  
  69.             return result;
  70.         }
  71.  
  72.         private static int IndexNotOf(string source, char[] array, int startIndex)
  73.         {
  74.             bool notFound = false;
  75.             for (int i = startIndex; i < source.Length; i++)
  76.             {
  77.                 for (int j = 0; j < array.Length; j++)
  78.                 {
  79.                     if (source[i] == array[j])
  80.                     {
  81.                         notFound = false;
  82.                         break;
  83.                     }
  84.                    
  85.                     notFound = true;
  86.                 }
  87.  
  88.                 if (notFound)
  89.                 {
  90.                     return i;
  91.                 }
  92.             }
  93.  
  94.             return source.Length;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement