C# :: Aufgabe #26

11 Lösungen Lösungen öffentlich

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.

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:

vote_ok
von pocki (4190 Punkte) - 29.12.2012 um 20:08 Uhr
Kurz und knapp meine Lösung:
Quellcode ausblenden 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);
}
vote_ok
von B.Denger (730 Punkte) - 03.09.2013 um 12:09 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von wladi-g (1310 Punkte) - 03.06.2014 um 10:49 Uhr
Quellcode ausblenden C#-Code
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]);
        }
    }
}
vote_ok
von hobeditz (650 Punkte) - 15.09.2014 um 14:41 Uhr
Ist vllt. nicht die eleganteste Lösung, aber sie ist leicht zu verstehen.

Quellcode ausblenden 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();
        }
    }
}
vote_ok
von DBqFetti (2480 Punkte) - 14.03.2015 um 20:24 Uhr
Quellcode ausblenden C#-Code
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);
		}
	}
}

vote_ok
von Torbo (880 Punkte) - 18.05.2015 um 13:36 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von niknik (1230 Punkte) - 07.08.2015 um 12:22 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von n.rohde (400 Punkte) - 12.08.2015 um 16:46 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von Smarc (60 Punkte) - 17.12.2016 um 22:26 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von stbehl (1640 Punkte) - 05.02.2018 um 09:17 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}
vote_ok
von stcalvin (970 Punkte) - 05.02.2018 um 13:40 Uhr
Quellcode ausblenden C#-Code
        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);
        }