罗马尼亚语文本是用乱码写成的,采用原始文本并在每个元音后插入字母 p 和相应的元音

Romanian text is written in gibberish by taking the original text and inserting after each vowel the letter p and the corresponding vowel

代码现在运行良好。有需要的可以随意使用。

问题:

编写一个控制台应用程序,将文本从乱码翻译成罗马尼亚语。乱码的语言类似于罗马尼亚语。罗马尼亚语文本是乱写的,采用原始文本并在每个元音后插入字母 p 和相应的元音。

示例:

对于输入数据:

Apanapa aparepe meperepe.

控制台会显示:

Ana are mere

这是我的代码:

using System;
using System.Text;

namespace FromGibberishUgly
{
    class Program
    {        
        private static string GibberishToRomanian(string text)
        {
            if (null == text)
                return "";

            const string vowels = "aeiouAEIOU";

            StringBuilder sb = new StringBuilder(text.Length);

            for (int i = 0; i < text.Length; ++i)
            {
                sb.Append(text[i]);                
                if (i < text.Length - 2 &&
                    vowels.Contains(text[i]) &&
                    text[i + 1] == 'p' &&
                    char.ToLower(text[i + 2]) == char.ToLower(text[i]))
                    i += 2;
            }

            return sb.ToString();
        }
        static void Main(string[] args)
        {               
            Console.WriteLine(GibberishToRomanian(Console.ReadLine()));
        }
    }
}

如果有简单模式(在你的情况下是vowel + p + vowel),你可以尝试使用正则表达式:

using System.Text.RegularExpressions;

...

private static string GibberishToRomanian(string text) =>  
  Regex.Replace(text ?? "", @"([aeiou])p", "", RegexOptions.IgnoreCase);

演示:

Console.Write(GibberishToRomanian("Apanapa aparepe meperepe"));

结果:

Ana are mere

模式说明:

([aeiou]) - capturing group #1 for any vowel
p         - letter 'p'
        - value captured by group #1

编辑:如果你想坚持循环,你可以尝试这样写:

private static string GibberishToRomanian(string text) {
  if (null == text)
    return "";

  const string vowels = "aeiouAEIOU";

  StringBuilder sb = new StringBuilder(text.Length);

  for (int i = 0; i < text.Length; ++i) {
    sb.Append(text[i]);

    // when facing vowel + p + vowel we jump over p + vowel 
    if (i < text.Length - 2 &&
        vowels.Contains(text[i]) &&
        text[i + 1] == 'p' &&
        char.ToLower(text[i + 2]) == char.ToLower(text[i]))
      i += 2;
  }

  return sb.ToString();
}

节目将是 (fiddle)

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace FromGibberishUgly {
  class Program {
    //TODO: or replace it with loop solution
    private static string GibberishToRomanian(string text) =>
      Regex.Replace(text ?? "", @"([aeiou])p", "", RegexOptions.IgnoreCase);

    static void Main(string[] args) {
      Console.WriteLine(GibberishToRomanian(Console.ReadLine()));
    }
  }
}