Home > Enterprise >  Romanian text is written in gibberish by taking the original text and inserting after each vowel the
Romanian text is written in gibberish by taking the original text and inserting after each vowel the

Time:12-25

The code works well now. Fell free to use it if you need it.

Problem:

To write a console application that translates a text from gibberish into Romanian. The gibberish language is similar to Romanian. A Romanian text is written in gibberish by taking the original text and inserting after each vowel the letter p and the corresponding vowel.

Example:

For input data:

Apanapa aparepe meperepe.

At the console it will show:

Ana are mere

Here is my code:

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()));
        }
    }
}

CodePudding user response:

When having a simple pattern (vowel p vowel in your case) you can try using regular expressions:

using System.Text.RegularExpressions;

...

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

Demo:

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

Outcome:

Ana are mere

Pattern explained:

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

Edit: If you want to stick to loops, you can try put it like this:

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();
}

The program will be (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\1", "$1", RegexOptions.IgnoreCase);

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