C :: Aufgabe #16 :: Lösung #1
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
#1

von devnull (8870 Punkte)
- 13.07.2013 um 18:40 Uhr

/******************************************** * vocals.c count vocals * * OS : GNU/Linux * compile: gcc -Wall -o vocals vocals.c * * devnull 13-07-2013 ********************************************/ #include <stdio.h> #include <ctype.h> #include <string.h> #define BUF_LEN 1000 const char vocals[] = {'?','A','E','I','O','U'}; /************ * functions ************/ /* read input line from stdin */ int readln( char *line ) { int len; if (fgets(line, BUF_LEN, stdin) != NULL) { line[BUF_LEN]='\0'; len=strlen(line); return len; } else return -1; } /* index in vocals array */ int ivocal( char c ) { int i; for (i=1;i<6;i++) if (toupper(c)==vocals[i]) return i; return 0; } /* count vocal frequency */ void count_vocals( char *buf, int *cvoc ) { while (*buf) (cvoc[ivocal(*buf++)])++; } /* print results */ void print_vocals( int *cvoc ) { int i,cv; for (i=1,cv=0;i<6;i++) { printf( "%c : %3d\n", vocals[i],cvoc[i] ); if (i>0) cv+=cvoc[i]; } printf( "Anzahl der Vokale: %d\n", cv ); } /* main */ int main() { char buffer[BUF_LEN+1]; int cvocals[6]; memset(cvocals,0,6*sizeof(int)); printf("Geben Sie einen Satz ein:\n"); if (readln(buffer) > 0) { count_vocals(buffer,cvocals); print_vocals(cvocals); } return 0; }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1