Add solution for stringaddierer.

This commit is contained in:
paulusja 2025-11-13 13:00:12 +01:00
parent 0db804eabc
commit 3ab40ce245

View File

@ -18,6 +18,7 @@
#include <string.h>
#include "../02_strings/eingabe.h"
#define UNBEKANNTES_ZAHLEN_WORT -1
#define MAX_EINGABE_LEN 100
char *wandleKlein(char *text);
@ -40,3 +41,63 @@ int main()
return EXIT_SUCCESS;
}
char *wandleKlein(char *text)
{
char *anfang = text;
while(*text != '\0')
{
*text = tolower(*text);
text++;
}
return anfang;
}
int wandleZahl(const char *wort)
{
const char *zahlenWoerter[] = {"null", "eins", "zwei", "drei", "vier",
"fuenf", "sechs", "sieben", "acht", "neun", "zehn"};
unsigned int anzahl = sizeof(zahlenWoerter)/sizeof(zahlenWoerter[0]);
for(int i = 0; i < anzahl; i++)
{
if(strcmp(wort, zahlenWoerter[i]) == 0)
return i;
}
return UNBEKANNTES_ZAHLEN_WORT;
}
int addiereText(const char *text, int *erg)
{
int istErfolgreich = 1;
const char *trenner = " \t";
char *wort;
char *kopie = malloc(sizeof(char)*(strlen(text)+1));
strcpy(kopie, text);
*erg = 0;
//wandleKlein(kopie);
wort = strtok(kopie, trenner);
while(wort != NULL)
{
int zahl = wandleZahl(wandleKlein(wort));
if(zahl == UNBEKANNTES_ZAHLEN_WORT)
{
istErfolgreich = 0;
break;
}
else
*erg += zahl;
wort = strtok(NULL, trenner);
}
free(kopie);
return istErfolgreich;
}