C# :: Aufgabe #137

6 Lösungen Lösungen öffentlich

Pig Latin - Schweine Latein

Anfänger - C# von DrizZle - 15.06.2016 um 13:57 Uhr
Hier zum Wikipedia Post

Einführung:

Zitat:

Pig Latin (engl.; wörtlich: Schweine-Latein) bezeichnet eine Spielsprache, die im englischen Sprachraum verwendet wird.
Sie wird vor allem von Kindern benutzt, aus Spaß am Spiel mit der Sprache oder als einfache Geheimsprache, mit der Informationen vor Erwachsenen oder anderen Kindern verborgen werden sollen.


Erklärung:

Zitat:

Beginnt das Wort mit einem Konsonanten, so wird der initiale Konsonant oder Konsonantencluster ans Ende des Wortes verschoben und ein „ay“ angehängt. Zu betrachten ist hierbei nicht die Rechtschreibung, sondern die tatsächliche Aussprache: „Stumme“ Konsonantenbuchstaben, wie z. B. das „h“ in „honor“, sind keine Konsonanten.

loser → oser-lay
button → utton-bay
star → ar-stay
three → ee-thray
question → estion-quay
happy → appy-hay
Pig Latin → Ig-pay Atin-lay

Beginnt das Wort mit einem Vokal oder einem stummen Konsonanten, so wird direkt ein „ay“ angehängt.

eagle → eagle-ay
America → America-ay
honor → honor-ay


Aufgabe:
Schreibt ein Programm welches ein belibiges Wort ins Schweine Latein umwandelt und ausgibt. Auf die Regel mit Stummen H's kann man verzichten.

Lösungen:

vote_ok
von daniel59 (4260 Punkte) - 21.07.2016 um 13:45 Uhr
Hier ist mein Programm. Ich habe zusätzlich noch die Übersetzung von Schweinelatein zu normalem Text hinzugefügt.

MainWindow.xaml.cs
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using Word = Microsoft.Office.Interop.Word;

namespace Schweinelatein
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Word.Application app;
        public MainWindow()
        {
            InitializeComponent();
            app = new Word.Application();
            textblockNToPig.Text = "\u2193 Übersetzen \u2193";
            textblockPigToN.Text = "\u2191 Übersetzen \u2191";
        }

        public string CreatePigLatin(string original, string pig)//Neues Wort erstellen, pig ist die Silbe
        {
            if (string.IsNullOrEmpty(original))
            {
                return null;
            }
            int i = original.Length;
            char konsonant = original.First(x => !(x != 'a' && x != 'e' && x != 'i' && x != 'o' && x != 'u' && x != 'ä' && x != 'ö' && x != 'ü' && x != '\0' && x != 'A' && x != 'E' && x != 'I' && x != 'O' && x != 'U' && x != 'Ä' && x != 'Ö' && x != 'Ü'));
            int index = original.IndexOf(konsonant);
            if (index > 0)
            {
                string cut = original.Substring(0, index).ToLower();
                string rest = original.Substring(index, original.Length - index);
                if (char.IsUpper(original[0]))
                {
                    rest = original.Substring(index, 1).ToUpper() + original.Substring(index + 1, original.Length - index - 1);
                }
                return rest + cut + pig;
            }
            else
            {
                return original + pig;
            }
        }

        public string CreatePigText(string original, string pig = "ay")//Erstelle für alle Wörter ein neues "Schweinewort"
        {
            List<KeyValuePair<string, char>> split = GotSplittedAt(original, ' ', ',', '!', '.', '=', '?', ';', '\r', '\n', '\t', '\0', '\"', '„', '“');
            string Out = null;
            foreach (KeyValuePair<string, char> el in split)
            {
                Out += CreatePigLatin(el.Key, pig) + el.Value;
            }
            Out = Out.Substring(0, Out.Length - 1);
            return Out;
        }

        public string CreateNormalText(string pigLatin, string pig)//Erstellt den Ursprungstext
        {
            List<KeyValuePair<string, char>> split = GotSplittedAt(pigLatin, ' ', ',', '!', '.', '=', '?', ';', '\r', '\n', '\t', '\0', '\"', '„', '“');
            string Out = null;
            foreach (KeyValuePair<string, char> el in split)
            {
                Out += CreateNormalWord(el.Key, pig) + el.Value;
            }
            Out = Out.Substring(0, Out.Length - 1);
            return Out;
        }

        public string GetCorrectWord(string pigLatin)//Findet das ursprüngliche Wort heraus, ohne Silbe
        {
            /*
            piglatin:   Schwein --> Einschway
            mögliche Wörter:
            Einschw
            Weinsch
            Hweinsc
            Chweins
            Schwein
            ...
            
            Das einzig tatsächlich existierende Wort ist Schwein,
            das auch zurückgegeben wird.
            */

            string text = pigLatin;
            bool isUpper = char.IsUpper(pigLatin[0]);
            List<string> words = new List<string>();
            for (int i = 0; i != pigLatin.Length; i++)
            {
                string word = text;
                if (isUpper)
                {
                    word = text.Substring(0, 1).ToUpper() + text.Substring(1, text.Length - 1).ToLower();
                }
                words.Add(word);
                text = text.Substring(text.Length - 1, 1) + text.Substring(0, text.Length - 1);
            }

            List<bool> correct = CheckSpelling(words).ToList();
            try
            {
                return words[correct.IndexOf(true)];
            }
            catch
            {
                return string.Format("%Error:[ {0} ]%", pigLatin);
            }
        }

        public string CreateNormalWord(string pigLatin, string pig)//Normales Wort aus Schweinelatein erstellen, pig ist die Silbe
        {
            if (!string.IsNullOrEmpty(pigLatin))
            {
                if (pigLatin.EndsWith(pig))
                {
                    string temp = pigLatin.Substring(0, pigLatin.Length - pig.Length);
                    return GetCorrectWord(temp);
                }
                else
                { return string.Format("%Error:[ {0} ]%", pigLatin); }
            }

            return null;
        }

        public IEnumerable<bool> CheckSpelling(IEnumerable<string> words)//Rechtschreibung prüfen
        {
            foreach (string word in words)
            {
                yield return app.CheckSpelling(word);
            }
        }

        public List<KeyValuePair<string, char>> GotSplittedAt(string Text, params char[] seperator)
        {
            List<KeyValuePair<string, char>> split = new List<KeyValuePair<string, char>>();
            char[] temp = Text.ToCharArray();
            string part = null;
            foreach (char el in temp)
            {
                if (!seperator.Contains(el))
                {
                    part += Convert.ToString(el);
                }
                else
                {
                    split.Add(new KeyValuePair<string, char>(part, el));
                    part = null;
                }
            }
            split.Add(new KeyValuePair<string, char>(part, '\0'));
            return split;
        }

        private void buttonTranslateNToPig_Click(object sender, RoutedEventArgs e)
        {
            string orig = new TextRange(richTextBoxOrig.Document.ContentStart, richTextBoxOrig.Document.ContentEnd).Text;
            string text = CreatePigText(orig, textBoxKey.Text);
            richTextBoxPig.Document.Blocks.Clear();
            richTextBoxPig.Document.Blocks.Add(new Paragraph(new Run(text)));
        }

        private void buttonTranslatePigToN_Click(object sender, RoutedEventArgs e)
        {
            string pig = new TextRange(richTextBoxPig.Document.ContentStart, richTextBoxPig.Document.ContentEnd).Text;
            string text = CreateNormalText(pig, textBoxKey.Text);
            richTextBoxOrig.Document.Blocks.Clear();
            richTextBoxOrig.Document.Blocks.Add(new Paragraph(new Run(text)));
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            app.Quit();
        }
    }
}


MainWindow.xaml
Quellcode ausblenden XML-Code
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Schweinelatein"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" x:Class="Schweinelatein.MainWindow"
        mc:Ignorable="d"
        Title="Schweinelateinübersetzer" Height="415" Width="525" ResizeMode="CanMinimize" Closing="Window_Closing">
    <Grid>

        <RichTextBox x:Name="richTextBoxOrig" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="492" Height="150" ToolTip="Normaler Text"/>
        <RichTextBox x:Name="richTextBoxPig" HorizontalAlignment="Left" Margin="10,223,0,0" VerticalAlignment="Top" Width="492" Height="150" ToolTip="Schweinelatein"/>
        <Button x:Name="buttonTranslateNToPig"  HorizontalAlignment="Left" Margin="159,168,0,0" VerticalAlignment="Top" Width="343" Click="buttonTranslateNToPig_Click">
            <TextBlock x:Name="textblockNToPig" Text="Übersetzen"/>
        </Button>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,174,0,0" TextWrapping="Wrap" VerticalAlignment="Top"><Run Text="Silbe"/></TextBlock>
        <TextBox x:Name="textBoxKey" TextWrapping="Wrap" Text="ay" Margin="41,170,363,184" TextAlignment="Center" Height="33"/>
        <Button x:Name="buttonTranslatePigToN" HorizontalAlignment="Left" Margin="159,196,0,0" VerticalAlignment="Top" Width="343" Click="buttonTranslatePigToN_Click">
            <TextBlock x:Name="textblockPigToN" Text="Übersetzen"/>
        </Button>
    </Grid>
</Window>
vote_ok
von Banger (50 Punkte) - 26.07.2016 um 08:57 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;


namespace PigLatin
{
    class Program
    {
        static List<string> StringList = new List<string>();                // Vokallist
        
        static void Main(string[] args)
        {
            StringList.Add("a");                                             // filling it          
            StringList.Add("e");
            StringList.Add("i");
            StringList.Add("o");
            StringList.Add("u");
                                                                                      // variables
            string SWord = null;                                            // starting string
            string EWord = null;                                            // substring
            string End = null;                                               // endsting

            int StringLength = 0;                                           // sting length
            int SubLength = 0;                                              // substing length
            int Index = -1;                                                    // substing Index

            Console.Write("Type your Word: ");
            SWord = Console.ReadLine();                                     // gets string
            
            foreach (string search in StringList)                           // gets the Index for the first Vokal in the string
            {
                Index = SWord.IndexOf(search);
                if (Index >= 0)
                    break;
            }

            End = SWord.Substring(0, Index).ToString() + "ay";              // uses the index to split string and rearrange

            StringLength = SWord.Length;
            SubLength = StringLength - Index;
            

            EWord = SWord.Substring(Index, SubLength );                     // builds the final sting

            Console.Write("Translation: ");                                 // console output
            Console.WriteLine(EWord + End);
            Console.ReadKey();
        }
    }
}
vote_ok
von Mexx (2370 Punkte) - 01.08.2016 um 19:29 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PigLatin
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Geben Sie ein Wort oder einen Text ein");
            string text = Console.ReadLine();
            string[] split = text.Split(' ');
            Console.WriteLine("");
            Console.WriteLine("Ergebnis:\n");

            foreach (string str in split)
            {
                Console.Write(Verschluesseln(str) + " ");
            }
            Console.ReadKey();
        }

        /// <summary>
        /// Prüft ob das übergebene Wort mit einem Vokal beginnt
        /// </summary>
        /// <param name="wort">Das zu prüfende Wort</param>
        /// <returns>true wenn das Wort mit einem Vokal beginnt, ansonsten false</returns>
        static bool IsVokal(string wort)
        {
            string vokale = "AaEeIiOoUu";
            if (vokale.Contains(wort[0]))
                return true;
            return false;
        }

        /// <summary>
        /// Prüft ob das übergebene Wort mit einem Konsunantencluster beginnt
        /// </summary>
        /// <param name="wort">Das zu prüfende Wort</param>
        /// <returns>Beginnt das Wort mit einem Konsunantencluster wird dieser zurück
        /// gegeben, ansonsten ein leerer String</returns>
        static string IsCluster(string wort)
        {
            string[] cluster2 = new string[] { "st", "qu" };     //Weitere Cluster aus zwei Buchstaben hier eintragen
            string[] cluster3 = new string[] { "thr", "sch" };   //Weitere Cluster aus drei Buchstaben hier eintragen

            foreach (string cluster in cluster2)
                if (wort.Substring(0, 2).ToLower() == cluster)
                    return cluster;

            foreach (string cluster in cluster3)
                if (wort.Substring(0, 3).ToLower() == cluster)
                    return cluster;

            return string.Empty;
        }

        /// <summary>
        /// Verschlüsselt das übergebene Wort in Pic Latin
        /// </summary>
        /// <param name="wort">Das zu verschlüsselnde Wort</param>
        /// <returns>
        /// Das verschlüsselte Wort 
        /// (konnte keine Verschlüsselung ermittelt werden, wird das Original zurück gegeben)
        /// </returns>
        static string Verschluesseln(string wort)
        {
            string cluster = string.Empty;
            if (IsVokal(wort))
            {
                return wort + "-ay";
            }
            else if ((cluster = IsCluster(wort)).Length > 0)
            {
                if (cluster.Length == 2)
                {
                    string sub = wort.Substring(2, wort.Length - 2);
                    return sub + "-" + cluster + "ay";
                }
                else if (cluster.Length == 3)
                {
                    string sub = wort.Substring(3, wort.Length - 3);
                    return sub + "-" + cluster + "ay";
                }
            }
            else
            {
                string sub = wort.Substring(1, wort.Length - 1);
                return sub + "-" + wort[0].ToString().ToLower() + "ay";
            }

            return wort;
        }
    }
}
1x
vote_ok
von Acel007 (170 Punkte) - 10.08.2016 um 18:12 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PigLatin
{
    class Program
    {
        static void Main(string[] args)
        {
            string word = "";
            while(word == "")   // hier wird überprüft, ob der User überhaupt ein Wort eingegeben hat; falls nicht, wird er erneut aufgefordert.
            {
                Console.WriteLine("Wort eingeben: ");
                word = Console.ReadLine(); // hier kann der User ein beliebiges Wort eingeben.
                Console.ReadKey();
            }
            string wordconv;
            wordconv = ConvertToPigLatin(word);
            Console.WriteLine(wordconv); // hier wird das Schweinewort ausgegeben.
            Console.ReadKey();   
        }
        static string ConvertToPigLatin(string convword)
        {
            char[] vocals = new char[] { 'a','e','i','o','u' };
            int vocal = convword.IndexOfAny(vocals); // hier wird ermittelt, wo sich das erste Vokal befindet.
            string secondconvword = convword.Substring(vocal); // hier wird die zweite Hälfte des Schweinewortes vom Ursprungswort abgeleitet.
            string firstconvword = convword.Remove(vocal); // hier wird die erste Hälfte aus dem Ursprungswort abgeleitet.
            string piglatinword = secondconvword + "-" + firstconvword + "ay"; // hier wird das Schweinewort gebildet.
            return piglatinword;
        }
    }
}
vote_ok
von Frevert (100 Punkte) - 09.02.2017 um 16:10 Uhr
Quellcode ausblenden C#-Code
static void Main(string[] args)
        {
            string a = Console.ReadLine();
            string latein = "";
            char[] word1 = a.ToCharArray();
            char[] vokal = { 'a', 'e', 'i', 'o', 'u','A','E', 'I', 'O', 'U' };
            int index = 0;

            if (vokal.Contains(word1[0]) | word1[0] == 'h')
                latein += a + "-";
            else
            {
                for (int i = 0; i < word1.Length; i++)
                {
                    if (vokal.Contains(word1[i]))
                    {
                        index = i;
                        break;
                    }
                }
                for (int i = index; i < word1.Length; i++)
                {
                    latein += word1[i];
                }
                latein += "-";
                for (int i = 0; i < index; i++)
                {
                    latein += word1[i];
                }
            }
            latein += "ay";
            Console.WriteLine(latein);
            Console.ReadKey();
        }     
vote_ok
von RBMelanie (20 Punkte) - 22.03.2018 um 20:21 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geheimsprache
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welches Wort möchtest Du umwandeln?");
            string wort = Console.ReadLine().ToUpper();
            char anfangsbuchstabe = wort.First();
            string ersteZweiBuchstaben = wort.Substring(0, 2);
            string ersteDreiBuchstaben = wort.Substring(0, 3);

            bool schleife = true;

       



            if (schleife == true)
            {
                schleife = CheckIfVocal(anfangsbuchstabe, wort);             
            }
            if (schleife == true)
            {
                schleife = CheckIfException3(ersteDreiBuchstaben, wort);
            }
            if (schleife == true)
            {
                schleife = CheckIfException(ersteZweiBuchstaben, wort);
            }
            if (schleife == true)
            {
                schleife = CheckIfConsonant(anfangsbuchstabe, wort);
            }

        }

        static bool CheckIfException3 (string firstThreeLetters, string word)
        {
            string exception = "SCH";
            bool loop = true;
            
                if (firstThreeLetters == exception)
                {
                    Console.WriteLine(word.Remove(0, 3) + firstThreeLetters + "AY");
                    Console.ReadKey();
                    loop = false;
                }

            return loop;

        }

        static bool CheckIfException(string firstTwoLetters, string word)
        {
            string[] exceptions = { "ST", "TH", "QU" };
            bool loop = true;
            for (int count = 0; count < 3; count++)
            {
                if (firstTwoLetters == exceptions[count])
                {
                    Console.WriteLine(word.Remove(0, 2) + firstTwoLetters + "AY");
                    Console.ReadKey();
                    loop = false;
                }
               
            }

            return loop;

        }

        static bool CheckIfConsonant(char firstLetter, string word)
        {
            char[] consonants = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };
            bool loop = true;
            for (int count = 0; count < 21; count++)
            {
                if (firstLetter == consonants[count])
                {
                    Console.WriteLine(word.Remove(0, 1) + firstLetter + "AY");
                    Console.ReadLine();
                    loop = false;
                }
                
            }
            return loop;
        }

        static bool CheckIfVocal(char firstLetter, string word)
        {
            char[] vocals = { 'A', 'E', 'I', 'O', 'U' };
            bool loop = true;
            for (int count = 0; count < 5; count++)
            {
                if (firstLetter == vocals[count])
                {
                    Console.WriteLine(word + "AY");
                    Console.ReadKey();
                    loop = false;
                }
               
            }
            return loop;
        }
    }
}