C :: Aufgabe #16 :: Lösung #2
5 Lösungen

#16
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
#2

von DBqFetti (2480 Punkte)
- 09.02.2015 um 20:38 Uhr

#include <stdio.h> #include <conio.h> int charcount(char text[], char character); int charcount(char text[], char character, bool casesensitive); int vocalcount(char text[]); int vocalcount(char text[], bool capitals); void main(){ char text[5000]; printf("Text>"); gets(text); printf("Anzahl der Vokale: %i\nA: %i\nE: %i\nI: %i\nO: %i\nU: %i", vocalcount(text), charcount(text, 'a'), charcount(text,'e'), charcount(text, 'i'), charcount(text, 'o'), charcount(text, 'u')); _getch(); }
vocalcount ist so gebaut, dass man mit true oder false nur auf große oder nur auf kleine Vokale prüfen lassen kann, möchte man alle, lässt man den letzten Parameter weg.
Das gleiche gilt für charcount, wenn man als letzten Parameter true angibt, wird casesensitive überprüft. Lässt man diesen weg oder gibt false an, dann nicht. Mit charcount kann man grundsätzlich jedes Zeichen zählen.

int charcount(char text[], char character){ int i = 0, count = 0; if(character < 91 && character > 64) character += 32; while(text[i] != '\0'){ if ((text[i] < 91 && text[i] > 64 ? text[i] + 32 : text[i]) == character) count++; i++; } return count; } int charcount(char text[], char character, bool casesensitive){ int i = 0, count = 0; if(!casesensitive && character < 91 && character > 64) character += 32; while(text[i] != '\0' && casesensitive){ if( text[i] == character ) count++; i++; } while(text[i] != '\0' && !casesensitive){ if ((text[i] < 91 && text[i] > 64 ? text[i] + 32 : text[i]) == character) count++; i++; } return count; }

int vocalcount(char text[]){ int i = 0, count = 0; while(text[i] != '\0'){ if(text[i] == 'a' || text[i] == 'A') count++; if(text[i] == 'e' || text[i] == 'E') count++; if(text[i] == 'i' || text[i] == 'I') count++; if(text[i] == 'o' || text[i] == 'O') count++; if(text[i] == 'u' || text[i] == 'U') count++; i++; } return count; } int vocalcount(char text[], bool capitals){ int i = 0, count = 0; if(capitals) while(text[i] != '\0'){ if(text[i] == 'A') count++; if(text[i] == 'E') count++; if(text[i] == 'I') count++; if(text[i] == 'O') count++; if(text[i] == 'U') count++; i++; } else while(text[i] != '\0'){ if(text[i] == 'a') count++; if(text[i] == 'e') count++; if(text[i] == 'i') count++; if(text[i] == 'o') count++; if(text[i] == 'u') count++; i++; } return count; }
Wäre natürlich nicht nötig gewesen, aber so wäre man letztendlich flexibler wenn man etwas ändern möchte.
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1