C# :: Aufgabe #26
11 Lösungen

Vokale zählen in einem beliebigen Satz
Anfänger - C#
von Dome
- 28.12.2012 um 23:58 Uhr
Programmieren Sie ein Programm, welches die Anzahl aller Vokale in einem zuvor eingegebenen Satz ausgibt.
Optional wäre die Ausgabe wie oft welcher Vokal in dem Satz vorhanden ist.
Optional wäre die Ausgabe wie oft welcher Vokal in dem Satz vorhanden ist.
Konsolenausgabe:
Geben Sie einen Satz ein :
Dies ist ein toller Satz.
Anzahl der Vokale : 8
A: 1
E: 3
I: 3
O: 1
U: 0
Lösungen:
Kurz und knapp meine Lösung:
C#-Code

void main() { Console.WriteLine("Geben Sie einen Satz ein:"); string input = Console.ReadLine(); int a = input.ToLower().Count(x=>x == 'a'); int e = input.ToLower().Count(x=>x == 'e'); int i = input.ToLower().Count(x=>x == 'i'); int o = input.ToLower().Count(x=>x == 'o'); int u = input.ToLower().Count(x=>x == 'u'); Console.WriteLine("Anzahl der Vokale: {0}", a+e+i+o+u); Console.WriteLine("A: {0}", a); Console.WriteLine("E: {0}", e); Console.WriteLine("I: {0}", i); Console.WriteLine("O: {0}", o); Console.WriteLine("U: {0}", u); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FakultätVonN { class Program { static void Main(string[] args) { Console.WriteLine("Geben sie einen Satz/Wort ein, dessen Vokale gezählt werden sollen"); string eingabe = Console.ReadLine(); int countA = eingabe.Split('A','a').Length - 1; int countE = eingabe.Split('E','e').Length - 1; int countI = eingabe.Split('I','i').Length - 1; int countO = eingabe.Split('O','o').Length - 1; int countU = eingabe.Split('U','u').Length - 1; int countAll = eingabe.Split('A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u').Length - 1; Console.WriteLine("Vokale Insgesamt : " + countAll); Console.WriteLine("A,a : " + countA); Console.WriteLine("E,e : " + countE); Console.WriteLine("I,i : " + countI); Console.WriteLine("O,o : " + countO); Console.WriteLine("U,u : " + countU); Console.ReadLine(); } } }

using System; using System.Linq; namespace VokaleZaehlen { class Program { static void Main(string[] args) { string eingabe; char[] vokale = new char[5] { 'a', 'e', 'i', 'o', 'u' }; int[] anzahl = new int[5]{0, 0, 0, 0, 0}; int summe = 0; Console.WriteLine("Geben Sie einen Satz ein :"); eingabe = Console.ReadLine(); eingabe = eingabe.ToLowerInvariant(); for (int i = 0; i < vokale.Length; i++) { while (eingabe.Contains(vokale[i])) { eingabe = eingabe.Remove(eingabe.IndexOf(vokale[i]), 1); anzahl[i]++; } } for( int i = 0; i < anzahl.Length; i++) summe += anzahl[i]; Console.WriteLine("Anzahl der Vokale : {0}\nA: {1}\nE: {2}\nI: {3}\nO: {4}\nU: {5}", summe, anzahl[0], anzahl[1], anzahl[2], anzahl[3], anzahl[4]); } } }
Ist vllt. nicht die eleganteste Lösung, aber sie ist leicht zu verstehen.
C#-Code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Vokale { class Program { static void Main(string[] args) { String eingabe; int anzahlA = 0; int anzahlE = 0; int anzahlI = 0; int anzahlO = 0; int anzahlU = 0; Console.WriteLine("Geben Sie einen Satz ein:"); eingabe = Console.ReadLine(); //Jedes Zeichen des Satzes einzeln in einem Array speichern. Char[] zeichen = eingabe.ToLower().ToCharArray(); //ToLower() -> auch Großbuchstaben sollen beachtet werden. //Jedes Zeichen durchgehen und prüfen ob ein Vokal vorliegt. for(int i = 0; i<zeichen.Length; i++) { char aktuellesZeichen = zeichen[i]; if (aktuellesZeichen == 'a') anzahlA++; else if (aktuellesZeichen == 'e') anzahlE++; else if (aktuellesZeichen == 'i') anzahlI++; else if (aktuellesZeichen == 'o') anzahlO++; else if (aktuellesZeichen == 'u') anzahlU++; } Console.WriteLine("Vokale : " + (anzahlA + anzahlE + anzahlI + anzahlO + anzahlU)); Console.WriteLine("A: " + anzahlA); Console.WriteLine("E: " + anzahlE); Console.WriteLine("I: " + anzahlI); Console.WriteLine("O: " + anzahlO); Console.WriteLine("U: " + anzahlU); Console.ReadLine(); } } }

using System; using System.Linq; namespace Vokale_in_Satz { class Program { static void Main(string[] args) { string satz; char[] vokale = new char[] { 'a', 'e', 'i', 'o', 'u' }; Console.Write("Geben Sie einen Satz ein>"); satz = Console.ReadLine(); Console.WriteLine("Anzahl der Vokale: {0}", satz.Count(c => vokale.ToList().Any(v => c.ToString().Equals(v.ToString(), StringComparison.OrdinalIgnoreCase)))); vokale.ToList().ForEach(v => Console.WriteLine("{0}: {1}", v.ToString().ToUpper(), satz.Count(c => c.ToString().Equals(v.ToString(), StringComparison.OrdinalIgnoreCase)))); Console.ReadKey(true); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace Vokale { class Program { static void Main(string[] args) { Console.WriteLine("Schreiben Sie einen Satz und wir zählen die Vokale"); string a = Console.ReadLine(); a = a.ToLower(); int b=Convert.ToInt32(Regex.Matches(a,"a").Count); int c=Convert.ToInt32(Regex.Matches(a,"e").Count); int d = Convert.ToInt32(Regex.Matches(a, "i").Count); int e = Convert.ToInt32(Regex.Matches(a, "o").Count); int f = Convert.ToInt32(Regex.Matches(a, "u").Count); Console.WriteLine("a ist "+ b +" vorhanden "); Console.WriteLine("e ist " + c + " vorhanden "); Console.WriteLine("i ist " + d + " vorhanden "); Console.WriteLine("o ist " + e + " vorhanden "); Console.WriteLine("u ist " + f + " vorhanden "); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Unterforderung { class Program { static void Main(string[] args) { string eingabe; int azahl = 0, ezahl = 0, izahl = 0, ozahl = 0, uzahl = 0, gesamt = 0; Console.WriteLine("Geben Sie einen Satz ein."); eingabe = Console.ReadLine().ToUpper(); for (int i = 0; i < eingabe.Length; i++) { if (eingabe[i] == 'A') { azahl++; gesamt++; } else if(eingabe[i] == 'E') { ezahl++; gesamt++; } else if (eingabe[i] == 'I') { izahl++; gesamt++; } else if (eingabe[i] == 'O') { ozahl++; gesamt++; } else if (eingabe[i] == 'U') { uzahl++; gesamt++; } } Console.WriteLine("Der Satz hat {0} Vokale", gesamt); Console.WriteLine("A: {0}", azahl); Console.WriteLine("E: {0}", ezahl); Console.WriteLine("I: {0}", izahl); Console.WriteLine("O: {0}", ozahl); Console.WriteLine("U: {0}", uzahl); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Vokale_zählen { class Program { static void Main(string[] args) { string satz; int anzahlA = 0, anzahlE = 0, anzahlI = 0, anzahlO = 0, anzahlU = 0; Console.WriteLine("Geben Sie einen Satz ein :"); satz = Console.ReadLine()+'\0'; for(int i=0;satz[i]!='\0';i++) { if (satz[i] == 'a') anzahlA++; else if (satz[i] == 'e') anzahlE++; else if (satz[i] == 'i') anzahlI++; else if (satz[i] == 'o') anzahlO++; else if (satz[i] == 'u') anzahlU++; } Console.WriteLine("Anzahl der Vokale : " + Convert.ToString(anzahlA + anzahlE + anzahlI + anzahlO + anzahlU)); Console.WriteLine("A: " + anzahlA); Console.WriteLine("E: " + anzahlE); Console.WriteLine("I: " + anzahlI); Console.WriteLine("O: " + anzahlO); Console.WriteLine("U: " + anzahlU); Console.Read(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Vokale_zählen { class Program { static void Main(string[] args) { string satz; int vocal = 0; int a = 0; int e = 0; int i = 0; int o = 0; int u = 0; int n = 0; Console.WriteLine("Bitte einen Satz eingeben: "); satz = Console.ReadLine(); foreach(char c in satz) { // if(satz[i] == 'a') switch(satz[n]) { case 'a': a++; break; case 'e': e++; break; case 'i': i++; break; case 'o': o++; break; case 'u': u++; break; case 'A': a++; break; case 'E': e++; break; case 'I': i++; break; case 'O': o++; break; case 'U': u++; break; } n++; } vocal = a + e + i + o + u; Console.WriteLine("Anzahl A: {0} ", a); Console.WriteLine("Anzahl E: {0}", e); Console.WriteLine("Anzahl I: {0}", i); Console.WriteLine("Anzahl O: {0}", o); Console.WriteLine("Anzahl U: {0}", u); Console.WriteLine("Anzahl Vokale: {0}", vocal); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer26 { class Program { static void Main(string[] args) { Console.WriteLine("Geben Sie einen Satz ein: "); string satz = Console.ReadLine(); int a = 0, e = 0, i = 0, o = 0, u = 0; while(satz.IndexOf('a') >= 0) { a++; satz = satz.Remove(satz.IndexOf('a'), 1); } while (satz.IndexOf('e') >= 0) { e++; satz = satz.Remove(satz.IndexOf('e'), 1); } while (satz.IndexOf('i') >= 0) { i++; satz = satz.Remove(satz.IndexOf('i'), 1); } while (satz.IndexOf('o') >= 0) { o++; satz = satz.Remove(satz.IndexOf('o'), 1); } while (satz.IndexOf('u') >= 0) { u++; satz = satz.Remove(satz.IndexOf('u'), 1); } Console.WriteLine("Anzahl der Vokale: " + (a+e+i+o+u)); Console.Write("A: " + a); Console.Write("\nE: " + e); Console.Write("\nI: " + i); Console.Write("\nO: " + o); Console.Write("\nU: " + u); Console.ReadKey(); } } }

static void Aufgabe_26() { string satz; char[] charSatz; int a = 0, e = 0, i = 0, o = 0, u = 0; Console.WriteLine("Geben Sie einen Satz ein :"); satz = Console.ReadLine(); charSatz = satz.ToCharArray(); for (int k = 0; k <= charSatz.Length - 1; k++) { switch (charSatz[k]) { case 'a': a++; break; case 'e': e++; break; case 'i': i++; break; case 'o': o++; break; case 'u': u++; break; default: break; } } Console.WriteLine("A: {0}", a); Console.WriteLine("E: {0}", e); Console.WriteLine("I: {0}", i); Console.WriteLine("O: {0}", o); Console.WriteLine("U: {0}", u); }