C# :: Aufgabe #124

3 Lösungen Lösungen öffentlich

Zeichensatz zu Umlauten

Fortgeschrittener - C# von J_U_B - 16.03.2016 um 10:03 Uhr
Es soll ein Programm geschrieben werden, welches Zeichensätze aus einem String - wie zum Beispiel "AE", "OE", "UE" oder "SS" - in die Entsprechenden Umlaute umwandelt - "AE" zu "Ä", "OE" zu "Ö" u.s.w.

Hierbei sollte auf die Korrekte Umsetzung der Wörter geachtet werden, hierfür einige Beispiele:

STOSSDAEMPFER = STOßDÄMPFER
STEUERKETTE = STEUERKETTE
KASSE = KASSE
HUELSE = HÜLSE

u.s.w.

Lösungen:

vote_ok
von daniel59 (4260 Punkte) - 06.04.2016 um 10:39 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using Word = Microsoft.Office.Interop.Word;

namespace ConsoleUmlaut
{
    class Program
    {
        static readonly string[] Umlaute = { "ae", "oe", "ue", "ss", "Ae", "Oe", "Ue" };

        static Dictionary<string, string> Values = new Dictionary<string, string>() { };
        static void Main(string[] args)
        {
            Values = new Dictionary<string, string>();
            Values.Add("ae", "ä");
            Values.Add("oe", "ö");
            Values.Add("ue", "ü");
            Values.Add("ss", "ß");
            Values.Add("Ae", "Ä");
            Values.Add("Oe", "Ö");
            Values.Add("Ue", "Ü");

            string text = "Stossdaempfer Steuerpruefung Suessstofffresssucht Uebergroessentraeger";
            string[] split = text.Split(' ', '\t', '\r', '\n', ',', ';', '.', '?', '!', '-');
            int words = split.Length;

            IEnumerable<IEnumerable<string>> allPossibilities = CreatePossibilities(split);
            IEnumerable<string> correctWords = GetCorrectWords(allPossibilities);

            foreach(string word in correctWords)
            {
                Console.WriteLine(word);
            }
            Console.ReadLine();
        }

        static IEnumerable<List<bool>> CheckSpelling(IEnumerable<IEnumerable<string>> words)
        {
            Word.Application app = new Word.Application();

            foreach (IEnumerable<string> word in words)
            {
                List<bool> list = new List<bool>();
                foreach (string s in word)
                {
                    list.Add(app.CheckSpelling(s));
                }
                yield return list;
            }

            app.Quit();
        }

        static IEnumerable<string> GetCorrectWords(IEnumerable<IEnumerable<string>> allPossibilties)
        {
            var checkedPossibilties = CheckSpelling(allPossibilties);
            int curList = 0;
            foreach (var item in checkedPossibilties)
            {
                int index = item.LastIndexOf(true);
                if (index == -1)
                { index = 0; }
                string word = allPossibilties.ElementAt(curList).ElementAt(index);
                curList++;
                yield return word;
            }
        }

        static IEnumerable<IEnumerable<string>> CreatePossibilities(string[] words)
        {
            foreach (string word in words)
            {
                yield return CreatePossibilties(word);
            }
        }

        static IEnumerable<string> CreatePossibilties(string word)
        {
            string[] split = SplitAtUmlaut(word);
            if (split == null)
            {
                yield return word;
            }
            else
            {
                int possibility = (int)Math.Pow(2, split.Length);
                bool[] getsMapped = new bool[split.Length];
                string[] temp = new string[split.Length];
                for (int i = 0; i != possibility; i++)
                {
                    split.CopyTo(temp, 0); ;

                    string s = Convert.ToString(i, 2).PadLeft(split.Length, '0');
                    var table = GetTable(s).ToList();
                    for (int b = 0; b != table.Count; b++)
                    {
                        if (table[b])
                        {
                            string part = split[b];
                            temp[b] = Replace(part);
                        }
                    }
                    string possible = RebuildString(temp);
                    yield return possible;
                }
            }
        }

        static string Replace(string text)
        {
            string key = text;
            foreach (var item in Values)
            {
                key = key.Replace(item.Key, item.Value);
            }
            return key;
        }

        static string RebuildString(string[] source)
        {
            string text = null;
            foreach (string s in source)
            {
                text += s;
            }
            return text;
        }

        static IEnumerable<bool> GetTable(string s)
        {
            char[] values = s.ToCharArray();
            for (int i = 0; i != values.Length; i++)
            {
                if (values[i] == '1')
                {
                    yield return true;
                }
                else if (values[i] == '0')
                { yield return false; }
                else
                { throw new ArgumentException("Der string darf nur die Zeichen '0' und '1' beinhalten."); }
            }
        }

        static string[] SplitAtUmlaut(string text)
        {
            List<int> indexes = FindAllIndexes(text, Umlaute).ToList();
            int minus = FindAllIndexes(text, new string[1] { "sss" }).Count();
            int count = indexes.Count - minus;
            if (count == 0)
            { return null; }
            else if (count == 1)
            { return new string[1] { text }; }
            else
            {
                string[] split = new string[count];
                int first = indexes.First();
                int last = indexes.Last();
                int length = 0;
                int startIndex = 0;
                for (int i = 0; i != count; i++)
                {
                    int next = 0;
                    if (i == count - 1)
                    { length = text.Length - startIndex; }
                    else if (i == 0 && indexes[i + 1] - indexes[i] == 1)
                    { length = indexes[1] + 1; next = length; }
                    else if (indexes[i + 1] - indexes[i] == 1)
                    { length = 2; next = startIndex + 2; }
                    else if (i == 0 && indexes[i] != 0)
                    { length = indexes[1]; next = length; }
                    else
                    { length = indexes[i + 1] - indexes[i]; next = indexes[i + 1]; }
                    split[i] = text.Substring(startIndex, length);
                    startIndex = next;
                }
                return split;
            }
        }

        static IEnumerable<int> FindAllIndexes(string source, params string[] param)
        {
            int index = 0;
            int i = 0;
            bool run = true;
            while (run)
            {
                List<int> temp = new List<int>();
                foreach (string par in param)
                {
                    temp.Add(source.IndexOf(par, i));
                }
                if (temp.Max() == -1)
                { break; }

                index = (from t in temp where t > -1 select t).Min();
                i = index + 1;
                yield return index;
            }
        }
    }
}
vote_ok
von Marat (50 Punkte) - 11.04.2016 um 19:34 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            convert convert = new convert();  //Wird objekt "convert" erstelt
            Console.WriteLine("Hier werden die umlauten \"AE\" zu \"Ä\" konventiert.\nSchreiben sie einen text:");
            string input = Console.ReadLine().ToUpper();
            convert.converter(ref input);
            if (input == "" || input == null)   //Wird gepüft ob "input" lehr oder nie benützt wurde.
                Console.ReadLine();
            else
            {
                Console.WriteLine("Ihr konventierter text:\n{0}", input.ToLowerInvariant());
                Console.ReadLine();
            }

        }
    }
    class convert
    {
        string text;

        public string converter(ref string text)
        {
            if (text.Contains("AE".ToUpper()))          //Wird kontroliert ob der text die buchstaben "AE" hat.
            {                                           //Wenn "true" dann wird "AE" zu "Ä" konventiert.
                return text = text.Replace("AE", "Ä");
            }
            else if (text.Contains("OE".ToUpper()))     //Wird kontroliert ob der text die buchstaben "OE" hat.
            {                                           //Wenn "true" dann wird "OE" zu "Ö" konventiert.
                return text = text.Replace("OE", "Ö");
            }
            else if (text.Contains("UE".ToUpper()))     //Wird kontroliert ob der text die buchstaben "UE" hat.
            {                                           //Wenn "true" dann wird "UE" zu "Ü" konventiert.
                return text = text.Replace("UE", "Ü");
            }
            else if (text == "" || text == null)        //Wenn es "false" ausgibt wird es eine fehlermeldung anzeigen.
            {
                Console.ForegroundColor = ConsoleColor.Red;
                string linie = new string('-', 49);
                Console.WriteLine(linie);
                Console.WriteLine("Kein buchstaben mit \"AE\", \"OE\" oder \"UE\" gefunden");
                Console.WriteLine(linie);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;
                return "";
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                string linie = new string('-', 49);
                Console.WriteLine();
                Console.WriteLine(linie);
                Console.WriteLine("Kein buchstaben mit \"AE\", \"OE\" oder \"UE\" gefunden");
                Console.WriteLine(linie);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;
                return "";
            }
        }
    }
}
1x
vote_ok
von eulerscheZhl (5230 Punkte) - 17.04.2016 um 08:26 Uhr
Da mir keine Regeln bekannt sind, schlage ich einfach auf duden.de nach.
Quellcode ausblenden C#-Code
using System;
using System.Net;

namespace TrainYourProgrammer
{
	class MainClass
	{
		static void Main(string[] args) {
			while (true) {
				Console.Write ("Wort eingeben: ");
				Console.WriteLine (CorrectWord (Console.ReadLine ()));
			}
		}

		static string CorrectWord(string word) {
			WebClient wbc = new WebClient ();
			string page = wbc.DownloadString ("http://www.duden.de/suchen/dudenonline/" + word);
			if (page.Contains ("class=\"hidden-link\">")) {
				page = page.Substring (page.IndexOf ("class=\"hidden-link\">") + "class=\"hidden-link\">".Length);
				return page.Substring (0, page.IndexOf ("</a>")).Replace ("\u00ad", "").Replace ("<strong>", "").Replace ("</strong>", "");
			}
			return "nicht gefunden";
		}
	}
}
1810118

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.