Info1-Uebungen/ub9/strAnalyzer.c

39 lines
945 B
C

#include <stdio.h>
#include <stdlib.h>
int stringLen(char **s, int *eCount) {
int count = 0;
int idx = 0;
*eCount = 0;
while (*(*s + idx) != '\0') { //(*(*s+idx)) == (*s)[idx]
if ((*s)[idx] == 'e') {
*eCount += 1;
}
count++;
idx++;
}
// newline (\n) am ende soll nicht gezaehlt werden deshalb count -1 falls
// count >0 ansonten 0 um zun verhindern dass negative stringLen
// zurueckgegeben weird falls count = 0
return count > 0 ? count - 1 : 0;
}
int main() {
char *str = NULL;
size_t size; // max allocated size of str (allocated by getdelim)
int numberEs;
int ret = getline(&str, &size, stdin);
if (ret == -1) { // getline failed. free allocated mem and return
free(str);
return 1;
}
printf("read: %s", str);
int len = stringLen(&str, &numberEs);
printf("it is %d chars long. It contains the letter \'e\' %d times\n", len,
numberEs);
free(str);
return 0;
}