C :: Aufgabe #16

5 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 devnull (8870 Punkte) - 13.07.2013 um 18:40 Uhr
Quellcode ausblenden C-Code
/********************************************
 * 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;
}
vote_ok
von DBqFetti (2480 Punkte) - 09.02.2015 um 20:38 Uhr
Quellcode ausblenden C-Code
#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.
Quellcode ausblenden C-Code
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;
}

Quellcode ausblenden C-Code
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.
vote_ok
von thet1983 (800 Punkte) - 05.11.2015 um 08:42 Uhr
Quellcode ausblenden C-Code
#include<stdio.h>
#include<string.h>

int main(){
	
	char satz[100];
	int vokale = 0;
	int a = 0;
	int e = 0;
	int i = 0;
	int o = 0;
	int u = 0;
	char c = 0;
	
	int k = 0;
	
	printf("Bitte geben Sie einen Satz ein: \n(max 100 Zeichen)\n");
	gets(satz);
	
	for(k = 0; k < strlen(satz) - 1; k++){
		c = satz[k];
		switch(c){
			case 'a': ++vokale; ++a; break;
			case 'e': ++vokale; ++e; break;
			case 'i': ++vokale; ++i; break;
			case 'o': ++vokale; ++o; break;
			case 'u': ++vokale; ++u; break;
		}
	}
	
	printf("\n\nAnzahl der Vokale: %d",vokale);
	printf("\nAnzahl a: %d",a);
	printf("\nAnzahl e: %d",e);
	printf("\nAnzahl i: %d",i);
	printf("\nAnzahl o: %d",o);
	printf("\nAnzahl u: %d",u);
	
	getchar();
	return 0;
}
vote_ok
von kathleenw (3600 Punkte) - 01.07.2020 um 10:31 Uhr
Quellcode ausblenden C-Code
#include <stdio.h>
#include <string.h>

int main(void)
{
    char satz[300];
    int vokale, a,e,i,o,u, k, c;
    
    printf("Bitte geben sie einen Satz mit max. 300 Zeichen ein: ");
    fgets(satz, 300, stdin);
    
    vokale = a = e = i = o = u = 0;
    
    for (k=0;k<strlen(satz);k++) {
        c = satz[k];
        switch (c) {
            case 'a' : vokale++;a++;break;
            case 'A' : vokale++;a++;break;
            case 'e' : vokale++;e++;break;
            case 'E' : vokale++;e++;break;
            case 'i' : vokale++;i++;break;
            case 'I' : vokale++;i++;break;
            case 'o' : vokale++;o++;break;
            case 'O' : vokale++;o++;break;
            case 'u' : vokale++;u++;break;
            case 'U' : vokale++;u++;break;
        }
    }
    
    printf("\nAnzahl der Vokale im Satz: %d",vokale);
    printf("\nAnzahl a: %d",a);
    printf("\nAnzahl e: %d",e);
    printf("\nAnzahl i: %d",i);
    printf("\nAnzahl o: %d",o);
    printf("\nAnzahl u: %d",u);
    
    getchar();
}
vote_ok
von psych0dad (260 Punkte) - 18.11.2020 um 11:47 Uhr
Quellcode ausblenden C-Code
/*************************************************************************
 *                                                                       *
 * Linux GCC                                                             *
 * 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.                          *
 *                                                                       *
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    char eingabe[100];
    size_t laenge;
    int i, A=0, E=0, I=0, O=0, U=0, allv=0;
    
    printf("Geben sie einen Satz ein: ");
    fgets(eingabe, 100, stdin);
    laenge=strlen(eingabe);
    

    for(i=0; i<laenge; i++){
        if(eingabe[i]=='a')
            A++;
        else if(eingabe[i]=='A')
            A++;
        else if(eingabe[i]=='e')
            E++;
        else if(eingabe[i]=='E')
            E++;
        else if(eingabe[i]=='i')
            I++;
        else if(eingabe[i]=='I')
            I++;
        else if(eingabe[i]=='o')
            O++;
        else if(eingabe[i]=='O')
            O++;
        else if(eingabe[i]=='u')
            U++;
        else if(eingabe[i]=='U')
            U++;
     }
    allv=A+E+I+O+U;

    printf("\n\nDer Satz enthaellt %d Vokale\n", allv);
    printf("A kommt %d Mal vor\n", A);
    printf("E kommt %d Mal Vor\n", E);
    printf("I kommt %d Mal vor\n", I);
    printf("O kommt %d Mal vor\n", O);
    printf("U kommt %d Mal vor\n", U);
    
    return EXIT_SUCCESS;
 }
2110085

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.